Angularjs ng-model在ng-if中不工作

前端之家收集整理的这篇文章主要介绍了Angularjs ng-model在ng-if中不工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这里是小提琴显示的问题。 http://jsfiddle.net/Erk4V/1/

如果我在ng-if里面有一个ng-model,模型就不能按预期工作。

我想知道这是一个错误,或者如果我误解了正确的使用。

<div ng-app >
    <div ng-controller="main">

        Test A: {{testa}}<br />
        Test B: {{testb}}<br />
        Test C: {{testc}}<br />

        <div>
            testa (without ng-if): <input type="checkBox" ng-model="testa" />
        </div>
        <div ng-if="!testa">
            testb (with ng-if): <input type="checkBox" ng-model="testb" />
        </div>
        <div ng-if="!someothervar">
            testc (with ng-if): <input type="checkBox" ng-model="testc" />
        </div>

    </div>
</div>
ng-if指令与其他指令一样创建一个子范围。看到这个小提琴: http://jsfiddle.net/Erk4V/4/

因此,您的复选框会更改子范围内的testb,而不会更改外父范围。

请注意,如果要修改父作用域中的数据,则需要修改对象的内部属性,如在我添加的最后一个div中。

猜你在找的Angularjs相关文章