在指令中,为什么我们不在customer.name
可以工作中添加$scope作为前缀?我换成$scope.customer.name
,不行?为什么?它应该有$scope,对吗?链接 (https://plnkr.co/edit/e4N2u1iuXbp7IESxNd1T?p=preview)
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}}'
};
});
您分配给template
的任何内容都等效于一个 HTML 页面,其正文包含您分配的内容。不要将其与角度js控制器中的代码混淆。它是字符串形式的 HTML 响应,我们不会在 HTML 文档中使用$scope
。