Q:

AngularJS update $scope from other controller

I have the following issue:
In my controller I have something like:
$scope.current = 0;

And I have some directive who is looking like this:
angular.module('myAPP')
    .directive('post', function() {
        return {
            restrict: 'EA',
            replace: true,
            scope: {
                post: '=',
                index: '='
            },

        link: function(scope) {
          //update the $scope.current here
        },
        templateUrl: 'components/blog/post.html'
    };
});

Can someone explain me whats the best way to do this, its possible to update the $scope inside the link function?
Thanks!

A:

Your directive has an isolate scope.
To change current from directive, you have to bind it with directive scope variable like this
Like this
html
<post index="current"></post>

js
link: function(scope) {
   scope.index=2;
},

A:

You need to use event handlers. It should look like this:
angular.module('myAPP')
    .directive('post', function() {
        return {
            restrict: 'EA',
            replace: true,
            scope: {
                post: '=',
                index: '='
            },

        link: function(scope) {
          $scope.on('my-event', function(value){
             $scope.current = value;
          });
          //update the $scope.current here
        },
        templateUrl: 'components/play/questions/simple-question.html'
    };
});

And in your controller you will send notification event to your directive:
$scope.emit('my-event', $scope.current);

If your directive lies outside of your controller, than you need to use $rootScope instead.

