我複製這個地方:AngularJS:在模板(用於指令)HTML的改變部分
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-包括但不創建一個新的工作範圍。
它可以像這樣使用:
<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屬性中的任何值進行交換?
編輯:我做了一個plunk ...
http://plnkr.co/edit/UxVbTq0Ik7VcTEzWNTxB?p=preview – 2014-09-10 12:46:52
其設置爲true上面,我將它設置爲false。 – 2014-09-10 12:47:24
哦,@NoypiGilas,完全忘了一件事,這就創造了一個新的範圍,這個指令的全部理由就是保持原來的範圍。有沒有辦法做到這一點,而不創建一個新的範圍? – EricC 2014-09-10 13:56:04