简体   繁体   English

角度指令中模板的路径?

[英]Path for templates in angular directive?

I have created a directive for my angular app, in the directive I am using templateUrl to get the template. 我已经为我的角度应用程序创建了一个指令,在指令中我使用templateUrl来获取模板。

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

This worked fine on my local server but when I host it on firebase it gives this warning repeatedly- 这在我的本地服务器上工作正常,但当我在firebase上托管它时,它会反复发出此警告 -

WARNING: Tried to load angular more than once

Also the page gets stuck in a loop, repeatedly adding index.html to page. 此外,页面陷入循环,反复将index.html添加到页面。 You can see the app here , if you go to the companies or jobs tab from the navigation bar which is where I have used the directive. 如果您从我使用该指令的导航栏转到公司或工作选项卡,您可以在此处查看该应用程序。 I am not able to figure out what path should I use to stop this. 我无法弄清楚应该使用什么路径来阻止它。 Here is the structure of the app - 这是应用程序的结构 -

.
├── 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

As you can see my template is in the templates folder. 如您所见,我的模板位于模板文件夹中。 Please help me with this. 请帮我解决一下这个。 Thank you. 谢谢。

Update - Accoring to this answer , the directive is not able to find the template hence loads the index.html again and again because of this - 更新 - 根据这个答案 ,该指令无法找到模板因此一次又一次地加载index.html - 因为 -

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

I just need to figure out the right path for the template. 我只需要弄清楚模板的正确路径。

This is contents of firebase.json json file - 这是firebase.json json文件的内容 -

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

Does '/' map to the view main.html ? '/'是否映射到视图main.html If so, it sounds like your server is only serving files statically under the views directory and not allowing access to the templates directory. 如果是这样,听起来您的服务器仅在views目录下静态提供文件,并且不允许访问templates目录。 To test it, try taking the template from templates and put it under views, then change the templateUrl accordingly. 要测试它,请尝试从模板中获取模板并将其放在视图下,然后相应地更改templateUrl

Just as you mentioned, Angular isn't able to access/find the directory so it defaults to the otherwise function, loading main.html which restarts the problem again and again ad infinitum. 正如您所提到的,Angular无法访问/查找目录,因此默认为otherwise功能,加载main.html会一次又一次地重新启动问题。

Edit: 编辑:

In case anyone else has this problem, the dist directory was the production version and used $templateCache to serve the views from a single file, but during the step in which the views were combined into one file the templates directory was not taken into consideration and so it was unable to be found. 如果其他人有这个问题, dist目录是生产版本并使用$templateCache从单个文件提供视图,但在将视图合并到一个文件的步骤中, templates目录没有被考虑在内所以无法找到它。 So in your Gruntfile.js , change src for templates to contain the other folder as well like this: 所以在你的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'
      }
    },

Don't redirect to index page. 不要重定向到索引页面。 because your app is running on index.html 因为你的应用程序在index.html上运行
So whenever you will redirect to index page the app will start again. 因此,无论何时您将重定向到索引页面,应用程序都将重新启动。
Instead of that use some another page with blank url and redirect to it. 而不是使用另一个页面与空白网址并重定向到它。
Like :- 喜欢 : -
create one js as main.js or home.js with blank url, like '/'. 创建一个js作为main.jshome.js与空白网址,如'/'。
So whenever you ll redirect to '/' your app will not initiate again and again. 因此,每当您重定向到“/”时,您的应用都不会一次又一次地启动。 and you ll not get any such warning. 你不会得到任何这样的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM