简体   繁体   English

Angular ui-router在头中找不到脚本模板

[英]angular ui-router can't find script template in head

I'm trying to inject a inline template into my body but I'm getting a 我正在尝试将内联模板注入我的体内,但是我得到了

GET http://localhost:3000/home.html 404 (Not Found) error in the console. 控制台中的GET http://localhost:3000/home.html 404 (Not Found)错误。

I've included the script in the head. 我把脚本放在了头上。

!!!
%html
  %head
    %title Movieseat

    %script{:id => "/home.html", :type => "text/ng-template"}
      .page-header
        %h1 Flapper News

  %body{"ng-app" => "movieseat"}

    %div{"ui-view" => ""}
    %a{"ui-sref" => "state1"} State 1
    %a{"ui-sref" => "state2"} State 2
    %a{"ui-sref" => "home"} home

And this is the route module, 这是路由模块,

angular.module('movieseat').config([

  '$urlRouterProvider', '$stateProvider', function($urlRouterProvider, $stateProvider) {
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    .state('home', {
      url: '/home',
      templateUrl: '/home.html',
      controller: 'searchCtrl'
    })

    .state('state1', {
      url: '/state1',
      templateUrl: 'assets/angular-app/templates/state1.html'
    })

    .state('state2', {
      url: "/state2",
      templateUrl: "assets/angular-app/templates/state2.html"
    })
  }

]);

Is there something wrong with the html > erb conversion on the script tag? 脚本标签上的html> erb转换是否有问题?

The issue is that your ng-app is on body so the script tag in head is outside of the scope of your main angular module. 问题在于您的ng-app位于body因此head中的script标签不在您的主要angular模块的范围内。

If templateUrl doesn't exist in $templateCache or in script tag ( also ends up in $templateCache), a request is made to server to retrieve it. 如果templateUrl在$ templateCache或脚本标记中不存在(也以$ templateCache结尾),则会向服务器发出请求以检索它。

Since your tag is outside scope of the app it is not found and therefore generates the server request. 由于您的标签不在应用程序的范围内,因此找不到该标签,因此会生成服务器请求。

Possible solutions: 可能的解决方案:

Move script tag inside body or move ng-app attribute to <html> tag 将脚本标签移到正文中或将ng-app属性移至<html>标签

After very carefully reading I saw I needed to put the script inside the body, and not in the head. 经过非常仔细的阅读,我发现我需要将脚本放入体内,而不是放在头部。 After putting it in the footer, with the rest of the scripts it works. 将其放在页脚中之后,其余的脚本都可以使用。

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

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