2017-05-01 39 views
0

我使用的WebPack有棱有角,我有一個HTML文件NG-包括:差異的行爲用NG-包括本地與生產

<ng-include src="stringUrl" ng-if="true"></ng-include> 

在控制器我設置stringUrl:

this.$scope.stringUrl = 'app/some-view.html'; 

用「的WebPack-dev的服務器」在本地運行時,該工作正常,但使用的WebPack的main.bundle.js文件不包含某些-view.html建設時。

所以,我試圖控制器更改爲:建築用的WebPack時

this.$scope.stringUrl = require('app/some-view.html'); 

所以,現在,一些-view.html裏面main.bundle.js。但是scope.stringUrl獲取HTML內容並且ng-include失敗(因爲它期待一個url)。

如果我刪除NG-包括,而是隻是把:

<div>{{stringUrl}}</div> 

它將在生產工作,但不會在本地工作,因爲stringUrl不會有HTML的內容。

任何想法如何解決這個問題?

在此先感謝

以色列

回答

0

我找到了一種方法開發&生產與ng-include標籤配合使用,$ templateCache。

首先需要注入$templateCache$templateCache: ng.ITemplateCacheService),所以我們可以在控制器/指令中使用它。

然後在$templateCache把(使用require)文件內容:

this.$scope.stringUrl = $templateCache.put("some-view.html", require("app/some-view.html")); 

在視圖文件,標籤ng-include應參照鍵(與'封裝URL):

<ng-include src="'some-view.html'"></ng-include> 

現在一切按預期工作。