What I'd like to do is very simple: two way bind a directive's scope. This means given a Parent controller ParentCtrl
with $scope.name='Foo'
and a directive with scope: {name: '='}
I'd expect that when I change scope.name='Bar'
from within the directive's link function or $scope.name='Bar'
from the given directive's controller I would expect that the the template for ParentCtrl would reflect the new $scope.name
, otherwise it's only one way bound from the parent scope to the directive's scope. Unfortunately that is not what I am getting. Am I missing something here? Here's a plunker link illustrating the issue:
http://plnkr.co/edit/VeGvWV2AGftFHF0LhnBY?p=preview
the code:
angular.module('docsSimpleDirective', [])
.controller('ParentCtrl', ['$scope', function($scope) {
$scope.name = "Bernie Sanders";
$scope.occupation = "Arsonist";
}])
.directive('myCustomer', function() {
return {
template: 'Name: {{name}} Occupation: {{occupation}}',
scope: {
name: '=',
occupation: '='
},
link: function(scope, element, attrs) {
scope.name = "Donald Drumpf";
scope.occupation = "Fascist pig";
scope.$apply();
}
};
});
the Template:
<body ng-app="docsSimpleDirective" ng-controller="ParentCtrl">
Name: {{name}}, Occupation: {{occupation}}<br />
<div ng-controller="ParentCtrl">
<div my-customer name="name" occupation="occupation"></div>
</div>
</body>