AngularJS directive does not update on scope variable changes

Armin

I've tried to write a small directive, to wrap its contents with another template file.

This code:

<layout name="Default">My cool content</layout>

Should have this output:

<div class="layoutDefault">My cool content</div>

Because the layout "Default" has this code:

<div class="layoutDefault">{{content}}</div>

Here the code of the directive:

app.directive('layout', function($http, $compile){
return {
    restrict: 'E',
    link: function(scope, element, attributes) {
        var layoutName = (angular.isDefined(attributes.name)) ? attributes.name : 'Default';
        $http.get(scope.constants.pathLayouts + layoutName + '.html')
            .success(function(layout){
                var regexp = /^([\s\S]*?){{content}}([\s\S]*)$/g;
                var result = regexp.exec(layout);

                var templateWithLayout = result[1] + element.html() + result[2];
                element.html($compile(templateWithLayout)(scope));
            });
    }
}

});

My problem:

When I'm using scope variables in template (in layout template or inside of layout tag), eg. {{whatever}} it just work initially. If I update the whatever variable, the directive is not updated anymore. The whole link function will just get triggered once.

I think, that AngularJS does not know, that this directive uses scope variables and therefore it will not be updated. But I have no clue how to fix this behavior.

QuarK

You should create a bound scope variable and watch its changes:

return {
   restrict: 'E',
   scope: {
     name: '='
   },
   link: function(scope) {
     scope.$watch('name', function() {
        // all the code here...
     });
   }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update angularJs directive scope variable on change parent scope

From Dev

AngularJS : directive does not update scope after $http response in parent scope

From Dev

AngularJS directive scope variable doesnt update after reopening the window

From Dev

Angularjs directive two-way bound variable changes are not triggering $digest on the parent scope

From Dev

AngularJS : update parent scope from directive

From Dev

AngularJS : Pass $scope variable as directive attribute

From Dev

AngularJs, $scope.variable is undefined in a directive

From Dev

Pass variable to AngularJS directive without isolated scope

From Dev

angularjs scope variable not updated outside directive

From Dev

Setting a scope variable from a directive with AngularJS

From Dev

angularjs scope variable not updated outside directive

From Dev

AngularJS $watch controller variable from a directive with scope

From Java

Update parent scope variable in AngularJS

From Dev

angular directive - Why does not update variable in directive?

From Dev

AngularJs: Why does scope inside my directive not update when async data arrives?

From Dev

Transcluded input ng-model does not update scope variable passed to directive

From Dev

variable does not get passed on to directive scope?

From Dev

AngularJS scope variable won't persist inside a directive with isolate scope

From Dev

why angularjs directive inherit the scope but doesn't change the scope variable?

From Dev

AngularJS scope variable won't persist inside a directive with isolate scope

From Dev

AngularJS $watch root scope variable for changes

From Dev

AngularJS not updating template variable when scope changes

From Dev

Update directive scope when attribute changes using controllerAs syntax

From Dev

i cant update variable via directive in angularjs

From Dev

i cant update variable via directive in angularjs

From Dev

AngularJS binding variable outside of scope and notify update?

From Dev

AngularJS how to dynamically update part of scope with variable

From Dev

AngularJS binding variable outside of scope and notify update?

From Dev

Angularjs Update Scope variable within setTimeout not working

Related Related

  1. 1

    Update angularJs directive scope variable on change parent scope

  2. 2

    AngularJS : directive does not update scope after $http response in parent scope

  3. 3

    AngularJS directive scope variable doesnt update after reopening the window

  4. 4

    Angularjs directive two-way bound variable changes are not triggering $digest on the parent scope

  5. 5

    AngularJS : update parent scope from directive

  6. 6

    AngularJS : Pass $scope variable as directive attribute

  7. 7

    AngularJs, $scope.variable is undefined in a directive

  8. 8

    Pass variable to AngularJS directive without isolated scope

  9. 9

    angularjs scope variable not updated outside directive

  10. 10

    Setting a scope variable from a directive with AngularJS

  11. 11

    angularjs scope variable not updated outside directive

  12. 12

    AngularJS $watch controller variable from a directive with scope

  13. 13

    Update parent scope variable in AngularJS

  14. 14

    angular directive - Why does not update variable in directive?

  15. 15

    AngularJs: Why does scope inside my directive not update when async data arrives?

  16. 16

    Transcluded input ng-model does not update scope variable passed to directive

  17. 17

    variable does not get passed on to directive scope?

  18. 18

    AngularJS scope variable won't persist inside a directive with isolate scope

  19. 19

    why angularjs directive inherit the scope but doesn't change the scope variable?

  20. 20

    AngularJS scope variable won't persist inside a directive with isolate scope

  21. 21

    AngularJS $watch root scope variable for changes

  22. 22

    AngularJS not updating template variable when scope changes

  23. 23

    Update directive scope when attribute changes using controllerAs syntax

  24. 24

    i cant update variable via directive in angularjs

  25. 25

    i cant update variable via directive in angularjs

  26. 26

    AngularJS binding variable outside of scope and notify update?

  27. 27

    AngularJS how to dynamically update part of scope with variable

  28. 28

    AngularJS binding variable outside of scope and notify update?

  29. 29

    Angularjs Update Scope variable within setTimeout not working

HotTag

Archive