0

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

  1. 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>
    
  2. 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>
    
0

2 Answers 2

1

You don't have to use $timeouts or $intervals to watch changes for certain scope values. Inside your directive you can watch for the changes of your login status via watching the user-login-status attribute.

DEMO

Something like this:

JAVASCRIPT

  .controller('LoginController', function($scope) {
    $scope.LoginStatus = "0";

    $scope.clickMe = function(){
       $scope.LoginStatus = "1";
    };
  })

  .directive('loginPanelDir', function() {
    return function(scope, elem, attr) {
      scope.$watch(attr.userLoginStatus, function(value) {
        console.log(value);
      });
    }
  });

HTML

<div id="login" login-panel-dir user-login-status="LoginStatus">
  <button id="btnLogin" type="submit" ng-click="clickMe()">Login</button>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

Working plunk. Use $timeout not setTimeout:

 setTimeout(function(){
       $scope.LoginStatus = "1";
    }, 3000); 

should be:

 $timeout(function(){
       $scope.LoginStatus = "1";
    }, 3000);   

setTimeout is native Javascript, so it will not run a digest and angular won't be notified of the changes hence no updates to the bindings, $timeout runs a digest when it completes, forcing an update to the binding.

Well its working here, just realized your $watch is also wrong: it should be:

scope.$watch("LoginStatus", function(){
    console.log('Login status  : '+scope.LoginStatus);  //will always return 0...          
});

5 Comments

I am using & as I am doing one way binding. The issue is not related to timeout at all, even if I change the variable in any way it will still not update the directive
Once again it is not related to setTimeout. Even if I totally removed setTimeout and tried to change the variable in controller upon button click or so, it will not update the directive
Did you try my other suggestions regarding the way you pass it to the directive?
Please read the question update first. Yes I have tried all what you have suggested and I can assure you it doesn't work
@MohammadSepahvand is correct in regards to using $timeout. Regardless of whether you instance of setTimeout works you should get in the habit of using angular options if they are available. Eventually it will catch up with you because angular compiles and stores everything independently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.