2016-09-04 149 views
5

我在我使用templateUrl獲取模板的指令中爲我的角度應用程序創建了一個指令。角度指令中模板的路徑?

templateUrl: 'templates/searchbox-template.html', 

這工作得很好我的本地服務器上,但是當我主持它的火力也給出了這樣的警告repeatedly-

WARNING: Tried to load angular more than once 

而且頁面陷在一個循環,反覆增加的index.html頁。 如果您從我使用過指令的導航欄中轉到公司或作業選項卡,則可以看到應用程序here。 我無法弄清楚我應該用什麼路徑來阻止它。這裏是應用程序的結構 -

. 
├── app 
│   ├── 404.html 
│   ├── favicon.ico 
│   ├── images 
│   ├── index.html 
│   ├── robots.txt 
│   ├── scripts 
│   ├── styles 
│   ├── templates 
│   └── views 
├── bower_components 
│   ├── angular 
│   ├── angular-mocks 
│   ├── angular-route 
│   ├── animate.css 
│   ├── bootstrap 
│   ├── jquery 
│   └── jssocials 
├── bower.json 
├── dist 
│   ├── 404.html 
│   ├── favicon.ico 
│   ├── fonts 
│   ├── index.html 
│   ├── robots.txt 
│   ├── scripts 
│   └── styles 
├── firebase.json 


. 
├── app 
│   ├── 404.html 
│   ├── favicon.ico 
│   ├── images 
│   ├── index.html 
│   ├── robots.txt 
│   ├── scripts 
│   │   ├── app.js 
│   │   ├── controllers 
│   │   ├── directives 
│   │   └── services 
│   ├── styles 
│   │   └── main.css 
│   ├── templates 
│   │   └── searchbox-template.html 
│   └── views 
│    ├── about.html 
│    ├── companiesall.html 
│    ├── company.html 
│    ├── contact.html 
│    ├── jobsall.html 
│    └── main.html 

正如你可以看到我的模板在模板文件夾。請幫我解決一下這個。謝謝。

更新 - Accoring這個answer,該指令是無法找到的模板,因此一次又一次加載,因爲這樣做的index.html的 -

.otherwise({ 
     redirectTo: '/' 
     }); 

我只需要找出正確的路徑爲模板。

這是firebase.json JSON文件的內容 -

{ 
    "hosting": { 
    "public": "dist", 
    "rewrites": [ 
     { 
     "source": "**", 
     "destination": "/index.html" 
     } 
    ] 
    } 
} 
+0

這是否模板包含偶然的NG-應用程序的指令?如果您已經在主模板中獲得了ng-app,然後嘗試加載另一個在其中具有ng-app的應用程序,則可能會發生此消息。 – stevenelberger

+0

不,ng-app只在index.html文件中。 – doctorsherlock

+0

取決於你的服務器配置,但是如果'/ /'指的是你的根目錄中的'index.html',那麼我會想象正確的路徑是'/ templates/searchbox-template.html'' – stevenelberger

回答

2

是否'/'映射到視圖main.html?如果是這樣,這聽起來像您的服務器只在views目錄下靜態提供文件,並且不允許訪問templates目錄。要測試它,請嘗試從模板中獲取模板並將其放在視圖中,然後相應地更改templateUrl

就像您提到的那樣,Angular無法訪問/查找目錄,因此它默認爲otherwise函數,正在加載main.html,從而無法再次重新啓動問題。

編輯:

如果任何人有這個問題,dist目錄是量產版本,並使用$templateCache服務從單個文件的意見,但在其中的意見合併成一步一個文件的templates目錄沒有考慮到,所以它無法找到。因此,在您Gruntfile.js,改變src爲模板,以包含其他文件夾,以及這樣的:

ngtemplates: { 
     dist: { 
     options: { 
      module: 'jobSeekerApp', 
      htmlmin: '<%= htmlmin.dist.options %>', 
      usemin: 'scripts/scripts.js' 
     }, 
     cwd: '<%= yeoman.app %>', 
     src: ['views/{,*/}*.html', 'templates/{,*/}*.html'], 
     dest: '.tmp/templateCache.js' 
     } 
    }, 
0

不要重定向到索引頁。因爲您的應用程序正在運行index.html
因此,無論何時您將重定向到索引頁面,應用程序都會重新啓動。
取而代之的是使用一些空白網址的其他網頁,並重定向到它。
: -
創建一個JS爲main.jshome.js空白網址,如 '/'。
因此,無論何時您將重定向到「/」,您的應用程序都不會一次又一次地啓動。你不會得到任何這樣的警告。