观察程序中的角度范围未更新



我有一个手表,在该手表中我检查一些值,然后更新另一个值。这是我的代码

scope.$watch(function () {
return userfactory.getUser();
}, function (newValue, oldValue) {
if (newValue.User) {

scope.domainuser = newValue.User.DomainUser;
scope.isGuardian = scope.domainuser.Category === 'Guardian';
if (scope.isGuardian && scope.mymessage !== undefined) {
angular.forEach(newValue.User.DependantUsers, function (value, key) {
if (scope.mymessage.OnBehalfOf === value.DomainUser.UserName) {
scope.mymessage.backGround = globalColorsArray[key];
console.log(scope.mymessage);
}

});
}
}
});

myMessage 是从调用指令的位置传递的。如果我有一个函数,让我们说一个点击按钮,如果我更新值,它确实会更新它。

奇怪的是,控制台日志中包含更新的属性。但是在模板上它没有得到它。

我的模板有很多东西,但问题就在这里

<span class="parent-child-color-scheme" ng-show="isGuardian" ng-class="mymessage.backGround"></span>

显然,如果我尝试更新任何属性,它在手表内不起作用。它不会生效。

这就是我在指令中的内容

return {
restrict: 'E',
scope: {
mymessage: "=message",
frontpage: "=",
inbox: "="
}, controller: function ($scope) {

看起来您在watch函数中的scope所指的与模板的要多。

如果使用监视侦听器函数的完整参数列表(如此处所述(,则可以确保使用正确的范围:

scope.$watch(function () {
return userfactory.getUser();
}, function (newValue, oldValue, scope) {
...

为了避免参数名称的阴影,您甚至可以使用不同的名称,例如:

}, function (newValue, oldValue, wScope) {
...
wScope.mymessage.backGround = globalColorsArray[key];

最新更新