安装或设置

指令附带 AngularJS 库本身。示例指令可以创建为:

angular.module('simpleDirective', [])
.directive('helloData', function() {
  return {
    template: 'Hello, {{data}}'
  };
});

并可用作:

JS:

angular.module('app', ['simpleDirective'])
.controller('Controller', ['$scope', function($scope) {
  $scope.data = 'World';
}])

HTML

<div ng-controller="Controller">
  <div hello-data></div>
</div>

将编译为:

Hello World