I have a directive that reads and writes attributes, but I'm having trouble getting it to work as expected. The issue seems to be with the controller inside main-directive.js, which is empty, while the actual action is happening in the isolated directive's controller.
You can view a live demo on Plunker: http://embed.plnkr.co/IkKPLahPc9yqeHWEQUG3/
In the main-directive.js file:
var app = angular.module('testapp.directive.main', ['main']);
app.directive('myCustomer', function() {
var controller = ['$scope', function($scope) {
$scope.dan = { 'name': 'Chad', 'nationality': 'China' };
// I want the scope.dan object to be read from here.
}];
var template = 'Getting attribute value of =getInfo... {{getInfo.name}} from {{getInfo.nationality}}';
return {
restrict: 'E',
controller: controller,
scope: {
getInfo: "=info"
},
template: template
};
});
app.controller('ctrl', function($scope) {
// adding the $scope.dan object here will work
// but I don't want it here.
});
And here's the template for it:
'mainview@': {
controller: 'MainCtrl as mainCtrl',
template: '<div ng-controller="ctrl"><my-customer info="dan"></my-customer></div>'
}
How can I ensure that the directive reads objects from its isolated controller rather than the 'ctrl' controller?
Thank you.