![](/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.