在angularjs JSON解析错误,从服务器获取,但不是一成不变的,当时 [英] JSON parsing error in angularjs when getting from server but not when static
问题描述
我有这个plunker:
http://plnkr.co/edit/FnCTfZf8RVBx2WVscyK8?p=info
I have this plunker: http://plnkr.co/edit/FnCTfZf8RVBx2WVscyK8?p=info
如果我更改行/秒(23左右)
if I change the line/s(around 23)
app.controller('MainCtrl', function($scope) {
$scope.links = [...];
});
到
app.controller('MainCtrl', function ($scope, $http) {
$http.get('data.json')
.success(function(data, status, headers, config) {
$scope.links = data;
});
我没有看到任何数据。
I don't see any data.
我想这是因为用户界面已经被渲染后的数据设置。结果
如何让数据绑定工作corectlly?
I guess this happens because the data is set after the ui has already been rendered.
How do I make the data binding work corectlly?
感谢
推荐答案
您所面临的问题是不同的变量引用。
即当你说
The issue you are facing is different reference of variables. i.e when you say
a = b
然后,当你修改B,A是不会改变的。因此,在NG-初始化你刚才用值进行初始化
Then when you modify "b", "a" is not going to change. Hence, in ng-init you have just initialized with value
submenu = links
在链接被更新则子菜单没有。
When "links" gets updated then "submenu" does not.
所以,在这里你可以对范围变量链接,它更新时,你可以更新子菜单设置手表。
So, here you can setup watch on scope variable "links", which when updated you can update "submenu".
请找到 plunkr 以相同。
code:
$scope.$watch('links',function(newValue){
$scope.submenu=newValue;
});
这篇关于在angularjs JSON解析错误,从服务器获取,但不是一成不变的,当时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!