I've seen directives created like so:
angular.module('docsSimpleDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
.directive('myCustomer', function() {
return {
template: 'Name: {{customer.name}} Address: {{customer.address}}'
};
});
The myCustomer
directive is now on the Controller
controller. If I define my module and controller and directive in different files like so:
docsApp.js:
angular.module('docsSimpleDirective', []);
docsController.js:
angular.module('docsSimpleDirective')
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
docsSimpleDirective.js:
angular.module('docsSimpleDirective')
.directive('myCustomer', function() {
return {
template: 'Name: {{customer.name}} Address: {{customer.address}}'
};
});
How do I wire myCustomer
into Controller
to be able to display <my-customer>
from in s
No, not at all. It has nothing to do with the controller. It just happens to expect a customer variable in the scope, and the controller happens to set such a variable in its scope. But you can use the directive whereever you want, in any view.
You use the controller and the directive the exact same way in your two examples. Whether they're defined in the same file or in different files doesn't change anything: as long as the files are both loaded by
<script>
tags of the html page, that will work the same way: the directive will display the customer of the controller scope if it's used inside a HTML element controller by the controller: