I'm trying to bind a variable inside a directive and modify it from the directive using the new Angular 1.4 bindToController syntax.
HTML:
<body ng-app="mainModule" ng-controller="mainController">
<h2>{{boundVar}}</h2>
<my-directive ng-model="boundVar"></my-directive>
Javascript:
angular.module('directiveModule', []).directive('myDirective',
function()
{
var r =
{
scope: true,
bindToController: { value: '=ngModel' },
template: '<h2>The value is: {{ctrl.value}}</h2><br><button ng-click="ctrl.increment()">Increment</button>',
controllerAs: 'ctrl',
controller: function()
{
this.increment = function() { this.value++; };
}
};
return r;
});
angular.module('mainModule', ['directiveModule']).controller('mainController',
function($scope)
{
$scope.boundVar = 10;
});
When the page is initially load, you see the variable 'boundVar' as effectively been bound:
But when you press the button, only the inner variable changes, so the two-way binding appears not to work:
What am I missing?
Thank you!