i am learning angular. so stuck to access controller scope from directive. $scope.vojta has been populated in controller which i like to print it from directive.
<div ng-controller="Controller">
<my-customer ></my-customer>
</div>
angular.module('docsIsolationExample', [])
.controller('Controller', ['$scope', function($scope) {
//$scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
$scope.vojta = { name: 'Vojta', address: '3456 Somewhere Else' };
}])
.directive('myCustomer', function() {
return {
restrict: 'E',
/*
scope: {
customerInfo: '=info'
},
*/
template: 'Name: {{vojta.name}} Address: {{vojta.address}}'
};
});
no name or address is printing. not being able to understand where i made the mistake. looking for some suggestion. here is fiddle https://jsfiddle.net/tridip/3g9yddf5/
As you have customerInfo: '=info'
inside isolated scope of directive, you should pass value from info
attribute(which is use as attribute alias), so that it can available with customerInfo
scope variable inside a directive.
<my-customer info="vojta"></my-customer>
And then change directive template to use customerInfo
which has passed customer info.
template: 'Name: {{customerInfo.name}} Address: {{customerInfo.address}}'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments