I have a directive and a controller in my AngularJS app as shown below, where I need the directive to be updated with the controller scope variable changes.
Problem I am facing is that any change to the controller scope variable do not update the directive. I've tried using {scope: false}, tried making an isolated scope and one-way binding with the controller scope variable as shown below but none worked, so can someone please check my code and let me know what I am missing / doing wrong here? Thanks
First trial using isolated scope in directive
.directive('loginPanelDir', function() { return { restrict: 'A', scope: { loginStatus: "&userLoginStatus" }, link: function(scope, element, attrs) { console.log(scope.loginStatus()); //will always print 0 despite of changes to the scope var in controller } }; }); .controller('LoginController', function ($scope, $location) { $scope.LoginStatus = "0"; $scope.clickMe = function(){ $scope.LoginStatus = "1"; }; }); <div id="login" login-panel-dir user-login-status="LoginStatus"> <button id="btnLogin" type="submit" ng-click="clickMe()">Login</button>Second trial using {scope:false} in directive
.directive('loginPanelDir', function() { return { restrict: 'A', scope: false, link: function(scope, element, attrs) { console.log(scope.LoginStatus()); //will always print 0 despite of changes to the scope var in controller scope.$watch(function(){ scope.LoginStatus }, function(){ console.log('Login status : '+scope.LoginStatus); //will always return 0... }); } }; }); .controller('LoginController', function ($scope, $location) { $scope.LoginStatus = "0"; $scope.clickMe = function(){ $scope.LoginStatus = "1"; }; }); <div id="login" login-panel-dir> <button id="btnLogin" type="submit" ng-click="clickMe()">Login</button>