2014-09-10 27 views
0

我複製這個地方: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 ...

+0

http://plnkr.co/edit/UxVbTq0Ik7VcTEzWNTxB?p=preview – 2014-09-10 12:46:52

+0

其設置爲true上面,我將它設置爲false。 – 2014-09-10 12:47:24

+0

哦,@NoypiGilas,完全忘了一件事,這就創造了一個新的範圍,這個指令的全部理由就是保持原來的範圍。有沒有辦法做到這一點,而不創建一個新的範圍? – EricC 2014-09-10 13:56:04

回答

1

我怎麼樣這個想法做一般模板的...... =)

該解決方案下面都沒有隔離範圍。 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"> 
+0

這是完美的@NoypiGilas,它非常簡單!非常感謝:) – EricC 2014-09-11 07:26:49

+0

很高興有幫助。 =) – 2014-09-11 07:38:00