对于如下所示的指令,是否有一种方法可以覆盖'templateUrl'属性,以便代替myTemplate1.tpl.html myTemplate2.tpl.html来使用?这是因为该指令已在其他地方使用,但是对于新要求,当前模板无法使用。
angular.module("myDirective", [])
.directive("myDirective", [function () {
"use strict";
return {
restrict: "E",
replace: true,
scope: {
"property1": "=",
"property2": "="
},
templateUrl: "web-url/myTemplate1.tpl.html"
};
}]);
如果您可以编辑指令代码本身,则可以执行以下操作:
angular.module("myDirective", [])
.directive("myDirective", [function () {
"use strict";
return {
restrict: "E",
replace: true,
scope: {
"property1": "=",
"property2": "="
},
templateUrl: function(element, attrs) {
// default to 'old' template
// only use other template if passed through the 'template-url' attribute
return attrs.templateUrl || "web-url/myTemplate1.tpl.html";
}
};
}]);
这样,您所有旧的指令用法仍然可以使用,您可以像这样配置它:
<!-- Still uses web-url/myTemplate1.tpl.html -->
<my-directive></my-directive>
<!-- Uses some/other/template/url.html -->
<my-directive template-url="some/other/template/url.html"></my-directive>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句