Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to execute additional code just after opening a new page using Angular JS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 388
    Comment on it

    In the cases when you execute any server and your attempts to load a new page view and then your codes in directive gets loaded before your services are updated so you can using $rootScope.
    This is an example of above case

    View

    <service-history log="log" data-ng-repeat="log in requiedData"></service-history>
    

    Controller

    app.controller("MyController",['$scope','$rootScope', function($scope, $rootScope) {
    
       $scope.$on('$viewContentLoaded', function () {
           SomeSerive.getHistory().then(function(data) {
               $scope.requiedData = data;
               $rootScope.$broadcast("history-updation");
           });
      });
    
    }]);
    

    Directive

    app.directive("serviceHistory", function() {
        return {
            restrict: 'E',
            replace: true,
            scope: {
               log: '='
            },
            link: function($scope, element, attrs) {
                function updateHistory() {
                   if(log) {
                       //do something
                   }
                }
                $rootScope.$on("history-updation", updateHistory);
            }
       };
    });
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: