2017-11-30 8 views
0

$ templateCacheスクリプトタグを使用してHTMLビューを表示します。 は現在、私は

var myApp = angular.module('myApp', ['editor']); 

myApp.config(['$httpProvider', function($httpProvider) { 
.... 
.... 
}]); 

myApp.controller('myAppController', ['$scope', '$http', '$rootScope', function ($scope, $http,$rootScope) { 
//Other stuff 
} 

directive.js

'use strict'; 

angular.module('editor').directive("my-settings", function() { 
    return { 
     templateUrl: '/views/templateId.html' 
    }; 
}); 

私はスクリプトタグを介して、またはサービスとして$ templateCacheを使用したい、

<body ng-app="myApp"> 
    <div ng-controller="myAppController"> 
    .... 
    .... 
    <div class="col-xs-12"> 
     <my-settings></my-settings> 
    </div> 

.. 
.. 
    </div> 

そして、私のapp.jsを持っています。しかし、これをサービス経由で追加するときは、すべてのHTML要素を$ templateCache.put()に入れなければなりません。

var myApp = angular.module('myApp', []); 
myApp.run(function($templateCache) { 
    $templateCache.put('templateId.html', 'This is the content of the template'); 
}); 

ここでスクリプトタグを使用します。ドキュメントから

<script type="text/ng-template" id="templateId.html"> 
    <p>This is the content of the template</p> 
</script> 

、それは、$ rootElementの子孫でなければなりません。だから私はスクリプトを配置する必要があります。これは同じファイル自体に<body ng-app="myApp">の中に含まれていますか?

答えて

1

あなたはディレクティブの名前を定義しながら、キャメルケースを使用する必要があります。

.directive("mySettings" ... 

また、あなたはこのように、ディレクティブのと同じスコープ内のインラインテンプレートを定義することができます。

<div ng-controller="myAppController"> 
    .... 
    .... 
    <div class="col-xs-12"> 
     <my-settings></my-settings> 
    </div> 

.. 
.. 
    <script type="text/ng-template" id="templateId.html"> 
     <p>This is the content of the template</p> 
    </script> 
    </div> 

インラインテンプレートのIDがtemplateUrlに一致するようにしてください。あなたは働くフィドルhereをチェックアウトすることができます。

+0

ありがとう..私はチェックします。また複数のスクリプトがあります。だから、私はメインのhtmlファイルの外のどこかに1つのファイルにまとめてもいいですか?では、どのようにして電話をかけることができますか? – NaaN

+0

@ep - 'templateUrl'は有効なテンプレート識別子でなければなりません。インラインテンプレートのIDか、http URLにすることができます。 AngularJSは自動的に内容を取り出して '$ templateCache'に格納します。 – 31piy

+0

もう1つ質問: ..スクリプトを同じように保つ必要がありますか? – NaaN

関連する問題