博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AngularJS ng-model在ng-if里面无效
阅读量:5150 次
发布时间:2019-06-13

本文共 1098 字,大约阅读时间需要 3 分钟。

参考stackflow原文。

问题:

Here is the fiddle showing the problem. 

It appears if I have an ng-model inside of an ng-if, the model does not work as expected.

I am wondering if this is a bug or if I am misunderstanding the proper usage.

Test A: {
{testa}}
Test B: {
{testb}}
Test C: {
{testc}}
testa (without ng-if):
testb (with ng-if):
testc (with ng-if):
回答:

The ng-if directive, like other directives creates a child scope. See this fiddle:

So, your checkbox changes the testb inside of the child scope, but not the outer parent scope.

Note, that if you want to modify the data in the parent scope, you'll need to modify the internal properties of an object like in the last div that I added.

 

 

个人解释:ng-if里面会生成一个子域,想要ng-model生效,需要在$scope创建一个子对象,才行,如$scope.obj,再将ng-model绑定到obj

使用ng-show(或ng-hide)可以间接解决这个问题。

 

转载于:https://www.cnblogs.com/leekenky/p/5021196.html

你可能感兴趣的文章