指令
ng-app 指令定义了 AngularJS 应用程序的 根元素,在网页加载完毕时会自动引导(自动初始化)应用程序。
ng-init 指令为 AngularJS 应用程序定义了 初始值。
ng-model 指令 绑定 HTML 元素 到应用程序数据。
你可以使用 .directive 函数来添加 自定义的指令。使用驼峰法来命名一个指令, runoobDirective, 但在使用它时需要以 - 分割, runoob-directive
例:<body ng-app="myApp">
< runoob-directive > < /runoob-directive >< script >
var app = angular.module("myApp", []);
app.directive("runoobDirective", function() {
return {
template : "<h1>自定义指令!</h1>"
};
});
< /script >
< /body >
你可以通过以下方式来调用指令:
- 元素名 .directive("runoobDirective", function() { return {
restrict : "C"template : "<h1>自定义指令!</h1> }; - 属性 .directive("runoobDirective", function() { return {
restrict : "C"template : "<h1>自定义指令!</h1> }; - 类名 .directive("runoobDirective", function() { return { restrict : "C" template : "<h1>自定义指令!</h1> };
- 注释 .directive("runoobDirective", function() { return { restrict : "M" template : "<h1>自定义指令!</h1> };
-
注:(E
只限元素名使用,A
只限属性使用,C
只限类名使用,M
只限注释使用。restrict 默认值为EA)