【问题标题】:why my html page is not getting render in <ui-view>?为什么我的 html 页面没有在 <ui-view> 中呈现?
【发布时间】:2016-05-09 19:16:09
【问题描述】:

我正在尝试用 rails 连接 angularjs。我的观点有一些问题。他们没有得到渲染

正在获取我的视图,但出现错误 [HTTP/1.1 404 Not Found]

我收到这个是对 first.html 的回应 https://github.com/sourabh-garg/vegeta/blob/master/response%20error.html

在我尝试加载的 app/assets/templates/first.html 中。

app/views/application/index.html.erb 有我的索引文件

路由 root 'application#index'

App.js

   var app = angular.module('myapp', ['ui.router'])

      .config(function config($stateProvider){

       $stateProvider.state("index", {
        url : "",
        controller : "FirstCtrl as first",
        templateUrl : "first.html"
       })

       $stateProvider.state("second", {
        url : "/second",
        controller : "SecondCtrl as second",
        templateUrl : "second.html"
       })

      })
      .directive('elem', function () {
        return {
          restrict: 'E',
          templateUrl: "templates/second.html"
        };
      })
      .filter('reverse', function(){

      return function(text){
        return text.split('').reverse().join('');
      }

      })
      .service("greeting", function Greeting(){

        var greeting =  this;

        greeting.message =  "Default";
       })
      .service('Data', [function () {

        return {message: "You are a awesome"}
      }])
      .controller('FirstCtrl', function($scope, greeting, Data){

        var first = this;
        first.greeting = greeting;

        first.data = Data;

        first.reversed = function(message){
          return message.split('').reverse().join('');

        };

      })
      .controller('SecondCtrl', function($scope, greeting){

        var second = this;
        second.greeting = greeting;
      });

【问题讨论】:

  • “/second”是否出现并正常工作?
  • 不,不是。我看到一个错误“templates/first.html”它应该只是“first.html”。即使在纠正之后。它不工作。
  • 您确定templateUrl: "templates/second.html" 指向正确的位置吗?不是templateUrl: "/app/assets/templates/second.html"
  • acutally 它只是“first.html”或“second.html”,因为我正在使用 rails-angular-template gem 它会自动查看 /app/assets/templates/

标签: javascript ruby-on-rails angularjs


【解决方案1】:

1) 您正在使用gem angular-rails-templates

您不必将模板模块注入到您的 Angular 应用程序中吗?
angular.module('myapp', ['ui.router', 'templates'])


2) 在您的文件结构 app/assets/javascripts/ 中,第 5 行的 application.js 文件中,查看将 //= require_tree . 更改为 //= require_tree ../templates 是否可以更正链接。

资源: angular-rails-template: include templates in rails assets pipeline

【讨论】:

  • 这可能是它。该错误使页面看起来像是被重定向到不存在的 Rails 端点,而不是 Angular 应用程序的路由端点。
  • 是的,正确。但即使在那之后它也没有工作。我必须将我的 html 文件保存在公用文件夹中,然后它才起作用。
【解决方案2】:

你的config函数写错了,试试

.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {

$stateProvider 被使用了两次。
// Now set up the states

// For any unmatched url, send to /route1 $urlRouterProvider.otherwise("/first");

$stateProvider .state('first', { url: "/first", templateUrl: "first.html" }) .state('second', { url: "/second", templateUrl: "second.html" }); }])

【讨论】:

  • 不,这不是问题。我也尝试过不命名该函数。但它没有用。
  • 只有我的视图没有得到渲染。如果我使用模板而不是 templateUrl,那么它工作正常。可能是 gem - rails-angular-template 的问题?
  • $stateProvider.state("second" 中删除$stateProvider 您已经启动了$stateProvider,第二个状态应该是链接。
  • 做到了。还是同样的问题。
  • 让我们试试.state('first', { url: "/first", templateUrl: "first.html" })
猜你喜欢
  • 2013-02-16
  • 2022-07-11
  • 1970-01-01
  • 2021-11-13
  • 2012-08-04
  • 2016-06-25
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多