我对angularjs相当陌生。我试图理解为什么使用这个指令比使用控制器更好。这两个示例都输出相同的值。了解角度控制器Vs指令
指令实施例:
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}}'
};
});
标记:
<div ng-controller="Controller">
<div my-customer></div>
</div>
控制器示例:
angular.module('docsSimpleDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.customer = {
name: 'Naomi',
address: '1600 Amphitheatre'
};
}])
标记:
<div ng-controller="Controller">
Name: {{customer.name}} Address: {{customer.address}}
</div>
也许我只是不完全理解指令。你需要
http://stackoverflow.com/questions/18757679/angularjs-directives-vs-controllers – scarto