我正在尝试从 Controller 更改 View ,但它不起作用。
app.js
var app = angular.module('vla', ['ngRoute']);
app.config(function ($routeProvider){
$routeProvider
.when('/view1',
{
controller: 'loginController',
templateUrl: 'partials/loginView.html'
})
.when('/view2',
{
controller: 'noteController',
templateUrl: 'partials/videoView.html'
})
.when('/view3',
{
controller: 'videoListController',
templateUrl: 'partials/videoListView.html'
})
.otherwise({ redirectTo: '/view1' });
});
查看:videoListView.html
<div class="video" data-ng-click="selectVideo(video)" ng-repeat="video in videos">
<div ng-repeat="(key, val) in video|orderBy:orderByField:videoName">
{{key}}: {{val}} </br>
</div>
</div>
Controller .js
app.controller('videoListController', function($scope,getVideoListService){
$scope.selectVideo = function(video){
$location.url('/view2');
}
});
我尝试了以下方法,但似乎都不起作用
$location.url('#/view2');
$location.url('/view2');
$location.path('/view2');
$location.path('#/view2');
在 View 页面上插入链接时,例如
<a href="#/view2"</a>click
页面路由正确,如果您能从 Controller 更改 View ,我们将不胜感激。
提前致谢
最佳答案
更新:
我之前应该已经看到过这个,但是 $location
是您没有传入的服务。
//Need to pass in $location
app.controller('videoListController', function($scope, $location, getVideoListService){
$scope.selectVideo = function(video){
$location.url('/view2');
}
});
为了帮助您进一步发展 Angular-foo,我会看一下 John Lindquist's fantastic videos at egghead.io 。大多数 Angular 视频都可以免费观看。
除此之外,Dependency Injection部分来自官方Angular Developers Guide这将是一本很好的读物。
根据您的描述,我的猜测是您的函数根本没有被调用。尝试添加一些控制台输出以查看它是否被调用,然后努力解决您的问题。
$scope.selectVideo = function(video){
console.log('Changing the route...');
$location.path('/view2');
}
打开开发者工具检查控制台输出是否有错误。
关于javascript - Angular Js - 更改 View 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22378537/