I have a directive with 2 way data binding scope, as following:
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);
}
}
});
In Template in my routing (using ui-router
and nested states) :
<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();
})
Any idea?
link
function only run once, if you want to deal with its new value, you can use scope.$watch
to 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
you can also use below code in your directive. scope.$apply(function() {});
see below link to for further information: https://thinkster.io/a-better-way-to-learn-angularjs/scope-vs-scope
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.