2016-04-26 3 views
0

このコードは動作しますngが、含まれています。私も'Test.html'"'Test.html'"に同じ結果を設定しました。使用変数

これを正常にng-include srcで使用する式に変換できますか?私は文字列がどのように解析されるかについていくつかの知識が不足していると思うが、私はかなり確信していた。'Test.html'はうまくいくはずだ。

答えて

0

これを確認してください。多分、fiddleがお手伝いします。

HTMLテンプレート:

<div ng-controller="Ctrl"> 
    <select ng-model="template" ng-options="t.name for t in templates"> 
    <option value="">(blank)</option> 
    </select> 
    url of the template: <tt>{{template.url}}</tt> 
    <hr/> 
    <div ng-include src="template.url" onload='myFunction()'></div> 
    </div> 

<!-- template1.html --> 
<script type="text/ng-template" id="template1.html"> 
    Content of template1.html 
</script> 

<!-- template2.html --> 
<script type="text/ng-template" id="template2.html"> 
    <p ng-class="color">Content of template2.html</p> 
</script> 

コントローラ:

function Ctrl($scope) { 
    $scope.templates = [{ 
     name: 'template1.html', 
     url: 'template1.html'}, 
    { 
     name: 'template2.html', 
     url: 'template2.html'}]; 
    $scope.template = $scope.templates[0]; 

    $scope.myFunction = function() { 
     $scope.color = 'red'; 
    } 
} 
+0

これは文字列変数を変更するまで機能します。私は自分のコードで 'Test1.html'にURLという文字列を定義し、この変数を$ scope.templates変数に割り当てます。私はそれを割り当てた後で動作しません。 – Lairinus

1

私は私自身の変数の名前を変更するにはあまりにも無能だということが分かりました。 ctrl.URLは本当にctrl.URlでした。今はすべてが機能しています。