gpt4 book ai didi

javascript - AngularJS 和 ui-router - templateUrl 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:24 25 4
gpt4 key购买 nike

我想在使用 ui-router 和 angular-rails-templates 时寻求有关 templateUrl 的帮助。我能够使"template"工作但不能使“templateUrl”工作。看起来它只是在重新渲染自己。这是屏幕截图:http://i.imgur.com/H3dnFoF.jpg

我打算做的是将 application.html.erb 设为主模板,然后从 app/assets/templates 切换多个 View 。我对此很陌生,似乎无法意识到我做错了什么。提前致谢!这是我的代码:

//app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
<head>
<title>WebApp</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body ng-app="webApp">
<div>
<h1>{{main}}</h1>
</div>
<div ui-view=""></div>
</body>
</html>

//应用程序/ Assets /javascripts/app.js

var appModule = angular.module('webApp', ['ui.router', 'templates']);

appModule.controller('IndexCtrl', [
'$scope',
function($scope){
$scope.main = '-Main Banner-';
}]);

appModule.controller('ContentCtrl', [
'$scope',
function($scope){
$scope.cont = 'Content page here!';
}]);

appModule.config(function (
$stateProvider,
$urlRouterProvider,
$locationProvider) {

$stateProvider
.state('home', {
url: '/',
templateUrl: 'homepage.html',
controller: 'IndexCtrl'
});

$stateProvider
.state('content', {
url: '/content',
templateUrl: 'content.html',
controller: 'ContentCtrl'
});

// default fall back route
//$urlRouterProvider.otherwise('/');

// enable HTML5 Mode for SEO
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});

});

//app/assets/templates/homepage.html.erb

<div class="container" ng-controller="IndexCtrl">
<h1>The Homepage View!</h1>
<h1>{{main}}</h1>
</div>

//app/assets/templates/content.html.erb

<div class="container">
<h1>The Content View!</h1>
</div>

//app/assets/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require angular
//= require angular-ui-router
//= require angular-rails-templates
//= app.js
//= require_tree . <- there's an error on initializing the module if this is removed
//= require_tree ../templates

//路由.rb

 root 'application#index'
get '*path' => 'application#index'

//应用程序 Controller

def index
render 'layouts/application'
end

最佳答案

看起来 app/assets/javascripts/application.js 需要更改。 //= app.js

//= require app

会起作用。

关于javascript - AngularJS 和 ui-router - templateUrl 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29779993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com