1

As the title says am trying to change scope variable from directive and from my controller. But it cannot change the value from controller/directive, here is a jsfiddle

If you click Change button it will change both scope variables from controller.

Then when clicking changed from controller button (which is now the directive), it will change drop variable but not the select variable and Change button cannot change the variable again.

I have also tried to pass the variable as described here but with no success.

Directive :

 app.directive("drop", [function() {
  return {
    scope: true,
    restrict: 'A',

    link: function(scope, element, attrs) {
      return element.bind('click', function(e) {
        scope.drop = "changed to drop";
        scope.select = "changed to select";
        scope.$apply();
      });
    }
  };
}]);

controller:

function myController($scope) {
  $scope.drop = "Initial value";
  $scope.select = "Initial Value";
  $scope.selection = function() {
    $scope.drop = "changed from controller";
    $scope.select = "changed from controller";
  };
}

Edit:

More testing shows that when changing the scope variable in directive, then controller cannot change it anymore..

Community
  • 1
  • 1
Zaki
  • 5,540
  • 7
  • 54
  • 91
  • 1
    You use a `scope: true`, which will create a child scope that prototypically inherits from the controller scope. Controller scope changes will reflect in the child scope, but not the other way around – devqon Feb 22 '16 at 12:31
  • @devqon yes removing scope:true resolves it but in my local project I am mirroring exactly what is in jsfiddle it wont work once the directive event is clicked... – Zaki Feb 22 '16 at 13:04

1 Answers1

1

remove the directive's scope attribute value, it gives the directive it's own scope

René
  • 662
  • 1
  • 4
  • 16
  • +1 It does resolve it for jsfiddle sample..In my local machine I am mirroring exactly what is in jsfiddle it wont work once the directive event is clicked. – Zaki Feb 22 '16 at 13:03
  • I am using requirejs not sure if that effects it in anyway – Zaki Feb 22 '16 at 13:03