我在某个地方复制了这个:
angular.module('app')
.directive('ffInclude', ['$http', '$templateCache', '$compile' ,function($http, $templateCache, $compile) {
return function(scope, element, attrs) {
var templatePath = attrs.ffInclude;
$http.get(templatePath, { cache: $templateCache }).success(function(response) {
var contents = element.html(response).contents();
$compile(contents)(scope);
});
};
}]);
这个指令的要点是像ng-include一样工作,但不创建新的范围。
它可以这样使用:
<div ff-include="my-template.html"></div>
我的问题如下:我想把它也用于一个通用模板,在这个模板中,我想切换出一些东西(html元素的一个属性)。
所以如果我有这个html:
<div ff-include="my-general-template.html" new-attribute-value="false"></div>
模板可能看起来像这样:
<div ng-show="true">Here is some general content</div>
在指令中,我应该得到这样的属性:
var newAttributeValue = attrs.newAttributeValue;
但是,如何将其与模板中ng-show属性中的任何值的新属性进行交换?
编辑:我做了一个砰的一声。。。
我喜欢这种做一般模板的想法…=)
下面的解决方案不会隔离范围。http://plnkr.co/edit/jFj8MHZ9Kalrk4qO35IX?p=preview
return function(scope, element, attrs) {
scope.newAttributeValue = attrs.newAttributeValue;
console.log("new=", scope.newAttributeValue);
在通用模板:
<div ng-show="newAttributeValue">