Make two Directives have the same $Scope at Angular

Asked

Viewed 1,033 times

3

I have a directive that generates a button, and when I click this button a counter increases 1. This directive is inserted in two places in my index.html. I wish that when I clicked on one of the buttons, the two would be changed...

Just follow my code:

index.html:

<div ng-app='demo'>
    <button-directive></button-directive>
    <button-directive></button-directive>
</div>

app js.:

var demo = angular.module('demo', []);
demo.directive('buttonDirective', function($parse) {
    return {
        restrict: 'E',
        template: '<button ng-controller="MyCtrl" ng-click="increment()">{{count}}</button>',
    }
});

demo.controller('MyCtrl', ['$scope', function($scope){
    $scope.count = 0;
    $scope.increment = function(){
        $scope.count = $scope.count + 1;
    };
}]);

Here is a fiddle: http://jsfiddle.net/dfalbel/1kyq5e15/

  • 1

    heightened the response

2 answers

3


I’m not very experienced with Angularjs, but as far as I know the default behavior of a directive is to share the controller’s Scope (otherwise we need to create an isolated Scope).

You are creating a new controller for each directive.

I would do so:

<div ng-app='demo' ng-controller="MyCtrl">
    <button-directive></button-directive>
    <button-directive></button-directive>
</div>

js:

var demo = angular.module('demo', []);
demo.directive('buttonDirective', function($parse) {    
    return {
        restrict: 'E',
        template: '<button ng-click="increment()">{{count}}</button>',
    }
});

demo.controller('MyCtrl', ['$scope', function($scope){
    $scope.count = 0;
    $scope.increment = function(){
        $scope.count = $scope.count + 1;
    };
}]);

https://jsfiddle.net/rvnypx1c/3/

But note that the directive became very specific. For it to be more generic and can be reused, we could do something like this:

<div ng-app='demo' ng-controller="MyCtrl">
    <button-directive ng-click="increment()" counter="{{count}}"></button-directive>
    <button-directive ng-click="increment()" counter="{{count}}"></button-directive>
    <button-directive ng-click="increment1()" counter="{{count1}}"></button-directive>
</div>

js:

var demo = angular.module('demo', []);
demo.directive('buttonDirective', function($parse) {

    return {
        restrict: 'E',        
        scope: {
            counter: '@'
        },
        template: '<button>{{counter}}</button>',
    }
});

demo.controller('MyCtrl', ['$scope', function($scope){
    $scope.count = 0;
    $scope.increment = function(){
        $scope.count = $scope.count + 1;
    };

    $scope.count1 = 0;
    $scope.increment1 = function(){
        $scope.count1 = $scope.count1 + 1;
    };
}]);

https://jsfiddle.net/rvnypx1c/5/

  • You’re right, I wanted to do it like this. the problem is that these buttons are in different places of my page... I can not put them inside the same div.

  • you can use the div or element that contains both, or in the worst case the body. Set that controller there that the Scope will be shared.

  • thank you, I managed using that same!

2

You can use the function $broadcast of $rootScope to transmit the events you wish to.

Code:

demo.controller('MyCtrl', ['$scope', '$rootScope', function ($scope, $rootScope) {
  $scope.count = 0;
  $scope.increment = function ($broadcast) {
    $scope.count++;
    $broadcast('evento', $scope.count);
  }
}
  • sorry, I still can’t make it work.. which means to broadcast an event?

  • Get the event sent to other controllers, who can receive the event and activate some function that receives this event.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.