Javascript 单击时角度视图未正确更新
我正在写选择按钮菜单,当我试图进入下一个选择级别时,什么也没发生 在第一个列表项中,我想写入下一级的选定值;在第二个列表项中,我已成功列出第一级的所有项目;在第三个列表项中,我想显示最后一级的第二级操作 我的代码如下所示:Javascript 单击时角度视图未正确更新,javascript,angularjs,angularjs-directive,angularjs-scope,twitter-bootstrap-2,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,Twitter Bootstrap 2,我正在写选择按钮菜单,当我试图进入下一个选择级别时,什么也没发生 在第一个列表项中,我想写入下一级的选定值;在第二个列表项中,我已成功列出第一级的所有项目;在第三个列表项中,我想显示最后一级的第二级操作 我的代码如下所示: <div ng-if="selected.type=='number' && selected.values[0].values"> <ul class="smsUl">
<div ng-if="selected.type=='number' && selected.values[0].values">
<ul class="smsUl">
<li ng-if="selected.selected">
<button class="btn btn-warning btn-block">{{selected.selected}}</button>
</li>
<li ng-repeat="value in selected.values" ng-if="attributes == undefined">
<button class="btn btn-info btn-block" ng-click="$parent.attributes=value.values">{{value.value}}</button>
</li>
<li ng-repeat="attribut in attributes" ng-if="attributes != undefined">
<button class="btn btn-info btn-block" ng-click="selected.value=attribut.id; selected.selected=attribut.value; attributes=undefined">{{attribut.value}}</button>
</li>
</ul>
</div>
[
{
"value": "clothing_glasses",
"values": [
{
"id": 1,
"value": "coat"
},
{
"id": 2,
"value": "glasses"
}
]
},
{
"value": "book_stationery",
"values": [
{
"id": 3,
"value": "book"
},
{
"id": 4,
"value": "document"
}
]
}
]
在我尝试使用的nf if表达式中:
!attributes
但一切都没有改变
更新:
如果我在控制器中静态设置属性变量,它会列出我想要的内容,但如果我选择,我应该返回到1。级别。您确定在控制器中没有执行类似操作:
$scope.attributes = [someArray]
(bla bla, some code)
$scope.someOnClickFunction = function() {
$scope.attributes = [otherArray]
}
$scope.$watchCollection('observable.attributes', function() {});
$scope.$watchCollection('observable.selected', function() {});
如果是这样的话,那么您就失去了正确变量的作用域,angular不再监视您认为应该监视的内容
我建议创建一些称为“可观察”的对象,将所有这些“属性”、“选定”等放在那里,并添加到控制器中:
$scope.attributes = [someArray]
(bla bla, some code)
$scope.someOnClickFunction = function() {
$scope.attributes = [otherArray]
}
$scope.$watchCollection('observable.attributes', function() {});
$scope.$watchCollection('observable.selected', function() {});
当您想更改这些数组中的某些内容时,不要通过将新数组应用于旧变量来替换它们。
如果您不想玩pushgin、Shift和poping元素,您可以始终使用:
someDefinedArray.length = 0;
for-loop {
someDefinedArray.push(yoursobjects); //push objects back to array but with changes you wanted to make to array
}
尝试使用Angular Batarang或ng inspector来确保$parent实际上是您所期望的父对象。我使用$parent解决了它。$parent.something,我的tpl文件有嵌套的foerach循环,因此我将原来的$scope留在了后面。