-1

Hello Folks : I have two directive , the second one is included in the template of the first one with two way binding

app.directive('A', function() {
  return {
    template : '{{ value}} <B model="value'><B>
    link : function( scope) {
        scope.value = null;
        scope.$watch('value', function(newValue) {
        console.log( newValue )
     });
    }
  }
}

And directive B where i do some action changing value

app.directive('B', function() {
  return {
    template : 'etc ...'
    scope : {
      model : '='
    },
    link : function(scope) {
       //  here i do some action changing
       scope.model = { bla bla bla }

    }
  }
}

When I do my action changing scope.model in B, the {{ value }} is updated in the template, but scope.value is not updated in the link no console log of new Value ... I can trigger event in B which might be called in A link, but I would like the scope.value to change in a link, do you have any elegant solutions?

Harigna
  • 11
  • 1
user2626210
  • 85
  • 4
  • 12
  • There is a mismatch of quotes in the template of directive `A`. The word `template` is misspelled in directive `B`. Otherwise there is nothing that would cause the problem. Perhaps the offending code was removed when editing. – georgeawg Jul 06 '18 at 13:39
  • In general two-way binding should be avoided. It makes migration to Angular 2+ difficult. See [AngularJS Developer Guide - Component-based application architecture](https://docs.angularjs.org/guide/component#component-based-application-architecture). – georgeawg Jul 06 '18 at 13:43
  • This question is not write since this exemple is working. Nevertheless i had a more complexe case where it wasn't working but i didn't mangage to reproduce it in simple terms. – user2626210 Jul 08 '18 at 15:21

1 Answers1

2

Add a controller and watch it inside that.

app.directive('A', function() {
  return {
    template : '{{ value}} <B model="value'><B>
    link : function( scope) { 
     },
     controller: function($scope){
        $scope.$watch('value', function(newValue) {
            console.log( newValue )
        })
     } 
  }
}
Sachila Ranawaka
  • 28,742
  • 4
  • 48
  • 69
  • The problem is more complexe than that. In these simple case, even with link the value is updated in the main directive. Nevertheless in my more complexe case it's not working. – user2626210 Jul 07 '18 at 16:00