AngularJS ng-model在ng-if内部不起作用

AngularJS ng-model在ng-if内部不起作用,第1张

AngularJS ng-model在ng-if内部不起作用

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>

因此,您的复选框将更改

testb
子作用域的内部,而不更改外部父作用域。

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



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5673990.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存