gpt4 book ai didi

javascript - $watch JSON 中的对象 Angularjs

转载 作者:行者123 更新时间:2023-11-29 10:34:00 25 4
gpt4 key购买 nike

我想在指令中为 json 对象的特定部分创建一个 $watch,但它似乎不接受语法(没有出现错误,但它永远不会进入 watch )

link: function (scope, element) {
scope.JsonObject={
profs:{
prof1:[{
name:example1a,
id:example1b
}],
prof2:[{
name:example2a,
id:example2b
}]
}
}

scope.teachers=scope.JsonObject['profs']

//until here all ok

for ( var teacher in scope.teachers){
//stuff to do
console.log("creating watch of " + teacher);

scope.$watch('teachers[teacher]', function() { //here seems to be the problem (it doesnt seem to accept JsonObject.teacher)
//stuff to do

}, true);
}

最佳答案

以前,teachers[teacher]undefined 因为没有 ' '(引号)围绕老师

for (var teacher in $scope.teachers){
$scope.$watch("teachers['"+ teacher +"']", function(newValue, oldValue) {
alert("changed");
}, true);
}

关于javascript - $watch JSON 中的对象 Angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39721241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com