![](/img/trans.png)
[英]Angular js assign custom created directives(for validation purpose) to a variable
[英]Angular - General Purpose Directives
我創建了一個通用directive
,希望在我們網站的任何Angular頁面中使用。 我不想強迫每個頁面使用相同的app
變量。 我將如何將該指令更改為通用指令,以便可以將其作為依賴項添加(當然也包括在單獨的js文件中)?
var app = angular.module('myapp', []);
app.directive('helloWorld', function() {
return {
restrict: 'AE',
replace: 'true',
scope: {
name: '@',
},
template: '<h3 >Hello {{name}}</h3> ',
}
};
});
您可以創建一個common
模塊,在其上聲明指令,並將其用作每個應用程序上的模塊依賴項。 通過在應用程序模塊依賴項上添加模塊,您將能夠使用指令以及在common
模塊上聲明的每個提供程序。
/common.module.js
;(function() {
var commom = angular.module('common', []);
commom.directive('helloWorld', function() {
return {
restrict: 'AE',
replace: true,
scope: {
name: '@',
},
template: '<h3 >Hello {{name}}</h3> ',
};
});
})();
/index.html
<html>
...
<body ng-app="myapp">
<hello-world name="World"></hello-world>
<script src="angular.min.js"></script>
<script src="common.js"></script>
<script>
var app = angular.module('myapp', ['common']);
</script>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.