我有一个具有 2 种数据绑定(bind)范围的指令,如下所示:
app.directive('channelStar', function() {
return {
restrict: "E",
template: 'This works fine : {{count}}',
scope: {
count: '='
},
link: function(scope, element, attrs) {
// count is showing in template, but doesn't get updated here
// actually it logs old scopes value
console.log(scope.count);
}
}
});
在我的路由中的 Template 中(使用 ui-router
和嵌套状态):
<channel-star count="selectedChannel[0].channel_dir_members"></channel-star>
Controller :
httpService.request(...).then(function(result){
$scope.selectedChannel = result.data;
$rootScope.selectedChannel = result.data;
// $scope.$applyAsync();
// $rootScope.$applyAsync();
})
有什么想法吗?
最佳答案
link
函数只运行一次,如果要处理它的新值,可以使用scope.$watch
watch count
.
scope.$watch('count', function(newValue, oldValue) {
if(newValue){
//do something
}
}, true); // here parameter true is for deep compare objects, no need to use it when just for string
关于javascript - Angularjs 指令的模板更新,但链接没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44135584/