[英]Confused about angular and rails routing together
I'm trying to integrate AngularJS into part of my app where I have anything under the url /events/something
be taken care of by angular routing. 我正在尝试将AngularJS集成到我的应用程序中,其中我在url /events/something
下有任何内容,可以通过角度路由来解决。 However, I've been following Angular's tutorial and this except I can't seem to get the view to render. 不过,我一直在关注角度的教程和这只是我似乎无法得到渲染视图。 I think I'm doing something wrong in my routing but I can't seem to figure it out. 我认为自己在路由中做错了,但似乎无法弄清楚。 Can anyone help me out? 谁能帮我吗?
Sample link: http://localhost:3000/events/events1
样本链接: http://localhost:3000/events/events1
Rails route: Rails路线:
get '/events/:id' => 'event#event'
Angular Code: 角度代码:
var app = angular.module('eventsApp', ['ngRoute', 'templates']);
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.when('events/:id', {
templateUrl: 'eventTemp.html',
controller: 'EventsCtrl'
});
}
]);
app.controller('EventsCtrl', function($scope, $http){
console.log('yo!'); //this is never logging
});
outer template: 外部模板:
<div class = "event" ng-app="eventsApp">
<div ng-view></div>
</div>
eventTemp.html eventTemp.html
<h1>Text</h1>
I'm using the angular-rails-templates
gem and I have all my templates in app/assets/javascripts/templates
. 我正在使用angular-rails-templates
gem,并且所有模板都在app/assets/javascripts/templates
。 This is what application.js
looks like: 这是application.js
样子:
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap-sprockets
//= require_tree .
//= require angular-rails-templates
//= require_tree ./templates
//= require turbolinks
I ran into a similar problem once, and the solution is this - use: 我曾经遇到过类似的问题,解决方法是-使用:
$routeProvider('/events/:id')
instead of 代替
$routeProvider('events/:id')
Apparently, angular requires is routing paths to start with /
. 显然,角度要求是路由路径以/
开头。
Is your Angular app in the same directory as the template? 您的Angular应用与模板位于同一目录吗? If not, templateUrl needs to be the correct path. 如果不是,则templateUrl必须是正确的路径。
templates/eventTemp.html
I played around with it and it turns out something is wrong with 我玩了一下,结果发现问题出在
$routeProvider.when('events/:id'
When I change the method to this: 当我将方法更改为此:
var app = angular.module('eventsApp', ['ngRoute', 'templates']);
app.config([
'$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: '../templates/eventTemp2.html',
controller: 'EventsCtrl2'
}).
otherwise({
templateUrl: '../templates/eventTemp.html',
controller: 'EventsCtrl'
});
}
]);
app.controller('EventsCtrl', function($scope, $http){
console.log('yo!');
});
app.controller('EventsCtrl2', function($scope, $http) {
console.log('yo2');
});
EventsCtrl2
and eventTemp.html2
get rendered. 将渲染EventsCtrl2
和eventTemp.html2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.