0

I have a directive, which watches for a inout box for old and new value. I would want to update the rootscope value here "total" . I tried to use all, that I knew like $rootScope, broadcast the message, emit the message. Here is the code.

app.directive('costCheck',function($compile,$rootScope,$timeout){
$rootScope.gName= "What did i buy?";
    return{
        restrict: 'A',
        link: function(scope,element,attrs){                    
            attrs.$observe('costCheck',function(value){ 

            });
            scope.$watch('cost',function(oldval,newval){alert(attrs.name);  
                alert(oldval+'--'+newval);
                var message = {type: 'channel', action: 'create', data: { name: "ssss", id: 0}};      
                $rootScope.$broadcast('get',message); 
            });     
        }
    }
});

This is my main controller

app.controller('MainCtrl', function($scope,$rootScope) {
 $scope.totalCost = 'workinggg';
 $rootScope.$on('go', function() {alert();
        $scope.totalCost = 'working';
    });
});

How to update the rootscope.

3
  • 1
    You're listening to go and broadcasting get. Commented Apr 7, 2014 at 14:20
  • If you only want to update the value you can pass it to the scope scope : { value : '=value'} and avoid nasty broadcast/emits. Commented Apr 7, 2014 at 14:21
  • I forgot to tell this in my question, the directive belongs below another controller Commented Apr 7, 2014 at 15:53

2 Answers 2

1

I agree with the comment from @haki that you should bind the scope values from the controller/directive as shown.

scope : { value : '=value'}

But if you are wanting to broadcast for other reasons (i.e. other controllers) then change $rootScope.$on to $scope.$on. $scope is a child of $rootScope and the broadcast sends the message down to child scopes.

Here is a fiddle showing the binding to the directive scope working.

Edit: fiddle update to set totalCost on $rootScope in the directive

Edit 2: fiddle update to broadcast value down from $rootScope, and handled in another controller

Sign up to request clarification or add additional context in comments.

10 Comments

I did not get you!, i tried doing this in ym directive but no luck!
@user3498603 I added a fiddle to explain the binding between the controller and the directive
This doesn't work for me, since the directive belongs to a different controller and with that I have to update the rootscope value.
I updated the fiddle to set $rootScope.totalCost directly inside the directive.
The 2nd fiddle IS updating $rootScope.totalCost which you can see here: jsfiddle.net/T8v6t/2 as the second binding in the h1 is outside of the controller. What error are you seeing?
|
0

There are many ways to accomplish this, and I wouldn't do it this way, but I'm just trying to make your code work here.

  • I guess you don't want to update the root scope (as the title says), but you want to communicate through the root scope.
  • As @Shomz said, you need to match the event names of course.
  • If you use $rootScope, you can just emit the event instead of doing a broadcast, then it doesn't have to bubble down in your app

Directive code:

$rootScope.$emit('totalCostUpdated',message); 

Main controller:

app.controller('MainCtrl', function($scope,$rootScope) {
   $scope.totalCost = 'workinggg';
   $rootScope.$on('totalCostUpdated', function(event, args) {
       $scope.totalCost = 'working';
   });
});

I would probably do it with a shared service, though. Take a look at https://egghead.io/lessons/angularjs-sharing-data-between-controllers .

Comments

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.