Update controller scope from directive

Brett

I am creating reusable UI components with AngularJS directives. I would like to have a controller that contains my business logic with the nested components (directives). I want the directives to be able to manipulate a single property on the controller scope. The directives need to have an isolate scope because I might use the same directive more than once, and each instance needs to be bound to a particular controller scope property.

So far, the only way I can apply changes back to the controller's scope is to call scope.$apply() from the directive. But this breaks when I'm inside of an ng-click callback because of rootScope:inprog (scope operation in progress) errors.

So my question: What is the best way to make my controller aware when a child directive has updated a value on the controller's scope?

I've considered having a function on the controller that the directive could call to make an update, but that seems heavy to me.

Here is my code that breaks on an ng-click callback. Keep in mind that I don't just want to solve the ng-click issue. I want the best overall solution to apply reusable directives to modify a parent scope/model.

html

<div ng-controller="myCtrl">
    <my-directive value="val1"></my-directive>
</div>

controller

...
.controller('myCtrl', ['$scope', function ($scope) {
    $scope.val1 = 'something';
}});

directive

...
.directive('myDirective', [function () {

return {
    link: function(scope) {
        scope.buttonClick = function () {
            var val = 'new value';
            scope.value = val;
            scope.$apply(); 
        };
    },
    scope: {
        value: '='
    },
    template: '<button ng-click="buttonClick()"></button>'
};
}]);
AlexMA

The purpose of two-way data binding in directives is exactly what you're asking about -- to "[allow] directives to modify a parent scope/model."

First, double-check that you have set up two-way data binding correctly on the directive attribute which exposes the variable you want to share between scopes. In the controller, you can use $watch to detect updates if you need to do something when the value changes. In addition, you have the option of adding an event-handler attribute to the directive. This allows the directive to call a function when something happens. Here's an example:

<div ng-controller="myCtrl">
    <my-directive value="val1" on-val-change="myFunc"> <!-- Added on-change binding -->
        <button ng-click="buttonClick()"></button>
    </my-directive>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to update the `$scope` object from `directive` controller

From Dev

Calling controller function from directive does not update scope

From Dev

Access controller scope from directive

From Dev

change controller $scope from a directive

From Dev

Update directive variable from controller

From Dev

Accessing directive scope from a separate controller

From Dev

Get value from directive to $scope in controller

From Dev

Passing data from directive to parent scope controller

From Dev

Change controller scope value from directive AngularJS

From Dev

Passing variable from controller scope to directive

From Dev

Passing controller scope from directive to partial

From Dev

Pass scope variable from directive to it's controller

From Dev

Angular moving scope variable from controller to directive

From Dev

AngularJS $watch controller variable from a directive with scope

From Dev

Watch on controller scope from directive link function

From Dev

angularjs - Watching service properties in a controller scope... changing them from a directive... no update?

From Dev

AngularJS directive controller's scope accessible from outside directive, why?

From Dev

How do I access a directive scope from a parent directive/controller?

From Dev

Not able to update the `scope` value from directive function

From Dev

AngularJS : update parent scope from directive

From Dev

Update the Parent Scope from Child Directive

From Dev

how to update a value from 'controller', in a 'directive'?

From Dev

Update scope name into directive

From Dev

update $scope info from another controller

From Dev

How to access to the controller scope from a directive inside ng-repeat?

From Dev

passing scope variables defined in http request from controller to directive

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

How to call angular controller scope function from directive with parameters?

From Dev

Passing object from parent controller to isolated scope child directive

Related Related

  1. 1

    How to update the `$scope` object from `directive` controller

  2. 2

    Calling controller function from directive does not update scope

  3. 3

    Access controller scope from directive

  4. 4

    change controller $scope from a directive

  5. 5

    Update directive variable from controller

  6. 6

    Accessing directive scope from a separate controller

  7. 7

    Get value from directive to $scope in controller

  8. 8

    Passing data from directive to parent scope controller

  9. 9

    Change controller scope value from directive AngularJS

  10. 10

    Passing variable from controller scope to directive

  11. 11

    Passing controller scope from directive to partial

  12. 12

    Pass scope variable from directive to it's controller

  13. 13

    Angular moving scope variable from controller to directive

  14. 14

    AngularJS $watch controller variable from a directive with scope

  15. 15

    Watch on controller scope from directive link function

  16. 16

    angularjs - Watching service properties in a controller scope... changing them from a directive... no update?

  17. 17

    AngularJS directive controller's scope accessible from outside directive, why?

  18. 18

    How do I access a directive scope from a parent directive/controller?

  19. 19

    Not able to update the `scope` value from directive function

  20. 20

    AngularJS : update parent scope from directive

  21. 21

    Update the Parent Scope from Child Directive

  22. 22

    how to update a value from 'controller', in a 'directive'?

  23. 23

    Update scope name into directive

  24. 24

    update $scope info from another controller

  25. 25

    How to access to the controller scope from a directive inside ng-repeat?

  26. 26

    passing scope variables defined in http request from controller to directive

  27. 27

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  28. 28

    How to call angular controller scope function from directive with parameters?

  29. 29

    Passing object from parent controller to isolated scope child directive

HotTag

Archive