【发布时间】:2016-02-12 19:49:59
【问题描述】:
这很奇怪,因为它应该很简单。我会先贴出我的代码,然后再提问:
html -
<div ng-controller="myController" ng-switch on="addressCards">
<div>
{{addCustom}} // does not get changed
<div ng-if="addCustom === false">
{{addCustom}} // does get changed
<button type="button" class="btn btn-primary btn-icon-text" ng-click="addCustom = true">
<span class="icon icon-plus"></span>
click here
</button>
</div>
</div>
</div>
控制器 -
(function(){
'use strict';
angular.module('myApp')
.controller('myController',['$scope',myController]);
function myController($scope){
$scope.addCustom = false;
}
})();
所以我只是在我的控制器中引入了一个范围变量 - addCustom - 并将其默认设置为 false。此变量控制是否显示 div。我还在 2 个不同的位置输出 html 上的范围值。请参见上文。
但是当我在这个 div 中通过 ng-click 更改它的值时,它的值在第二个位置(在 div 内)发生变化,而不是在第一个位置(在 div 外)。因此,div 也不会改变状态。
我无法弄清楚这里可能有什么问题。有人可以帮忙吗?
【问题讨论】:
标签: angularjs angularjs-scope prototypal-inheritance angular-ng-if