Angularjs ng-model在ng-if中不起作用

Jus*_*son 201 angularjs

这是显示问题的小提琴.http://jsfiddle.net/Erk4V/1/

如果我在ng-if中有ng模型,则该模型似乎不能按预期工作.

我想知道这是一个错误还是我误解了正确的用法.

<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>
Run Code Online (Sandbox Code Playgroud)

Jon*_*on7 218

ng-if与其他指令一样,该指令会创建子范围.看下面的脚本(或这个jsfiddle)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular.min.js"></script>

<script>
    function main($scope) {
        $scope.testa = false;
        $scope.testb = false;
        $scope.testc = false;
        $scope.obj = {test: false};
    }
</script>

<div ng-app >
    <div ng-controller="main">
        
        Test A: {{testa}}<br />
        Test B: {{testb}}<br />
        Test C: {{testc}}<br />
        {{obj.test}}
        
        <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" /> {{testb}}
        </div>
        <div ng-if="!someothervar">
            testc (with ng-if): <input type="checkbox" ng-model="testc" />
        </div>
        <div ng-if="!someothervar">
            object (with ng-if): <input type="checkbox" ng-model="obj.test" />
        </div>
        
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

因此,您的复选框会更改testb子范围内部,但不会更改外部父范围.

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

  • 这是在范围内使用包含对象相当普遍的一个原因,而不是直接修改范围属性,如示例中所示:`$ scope.obj = {...}`和`ng-model ="obj .someProperty"`克服了这个限制. (21认同)

zs2*_*020 203

您可以使用它$parent来引用父作用域中定义的模型

<input type="checkbox" ng-model="$parent.testb" />
Run Code Online (Sandbox Code Playgroud)

  • 然后我有'ng-model ="$ parent.$ parent.foo`因为我已经在一个带有'ng-repeat`的范围内 - 这真的是最好的方法吗? (16认同)
  • 回复@Gaul:大概是因为ng-hide/ng-show在现有的DOM上运行,只是添加/删除一个CSS类,而ng-if/ng-switch/ng-repeat对DOM的所有混乱并跟踪额外的状态.似乎是明智的. (5认同)
  • 这真的令人困惑.这是为什么?为什么ng-hide没有自己的范围? (4认同)
  • 明智的是**不是**我用的词. (4认同)
  • 将对象添加到原始范围,并更改该对象的属性.例如ng-model ="myObject.property".这将回避所有范围/ $ parent inanity.谷歌"角点规则"获取更多信息. (3认同)

Vas*_*tin 47

您可以使用ngHide (或ngShow)指令.它不像ngIf那样创建子范围.

<div ng-hide="testa">
Run Code Online (Sandbox Code Playgroud)

  • 注意zsong回答的评论.`ng-hide`不会改变html结构.它只是改变了CSS样式.`ng-if`更复杂:它根据条件删除并插入html部分.它创建子存储状态以存储状态(至少它应该存储隐藏的html部分). (5认同)
  • 为什么`ngIf`然后创建子范围?对我来说似乎很奇怪. (3认同)

小智 7

在许多其他情况下我们有这个,我们内部决定总是有一个控制器/指令的包装器,这样我们就不需要考虑它了.这是你的包装器的例子.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular.min.js"></script>

<script>
    function main($scope) {
        $scope.thisScope = $scope;
        $scope.testa = false;
        $scope.testb = false;
        $scope.testc = false;
        $scope.testd = false;
    }
</script>

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

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

        <div>
            testa (without ng-if): <input type="checkbox" ng-model="thisScope.testa" />
        </div>
        <div ng-if="!testa">
            testb (with ng-if): <input type="checkbox" ng-model="thisScope.testb" />
        </div>
        <div ng-show="!testa">
            testc (with ng-show): <input type="checkbox" ng-model="thisScope.testc" />
        </div>
        <div ng-hide="testa">
            testd (with ng-hide): <input type="checkbox" ng-model="thisScope.testd" />
        </div>

    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Yishay,希望这会有所帮助