gpt4 book ai didi

AngularJS 路由不工作

转载 作者:行者123 更新时间:2023-12-02 00:16:02 25 4
gpt4 key购买 nike

我试图从 Angular JS 制作“入门”教程,但在设置链接的路由时遇到问题:当用户单击链接时,该路由的 Controller 不会被调用。

这是我的代码:

angular.module('phonecat', []).
config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/phones', {
templateUrl: 'partials/phone-list.html',
controller: PhoneListCtrl
}).
when('/phones/:phoneId', {
templateUrl: 'partials/phone-detail.html',
controller: PhoneDetailCtrl
}).
otherwise({
redirectTo: '/phones'
});
}
]);

function PhoneDetailCtrl($scope, $routeParams, $http) {
$scope.phoneId = $routeParams.phoneId;

$scope.total = 4;
$http.get('phones/' + $routeParams.phoneId + '.json').success(function (data) {
$scope.phone = data;
});
}

function PhoneListCtrl($scope, $http) {
$http.get('phones/phones.json').success(function (data) {
$scope.phones = data;
});

$scope.orderProp = 'age';
}

最佳答案

你用的是哪个angularjs版本?我也遵循教程,它使用 angular-1.0.0rc7.js,如果我在 app.js 文件中查找,它使用 template 而不是 templateUrl:

$routeProvider.when('/phones', {
template: 'partials/phone-list.html',
controller: PhoneListCtrl
});

因此,通过查看您的代码,您可能希望使用 angular-1.0.3.js 或 RC 之上的先前版本

关于AngularJS 路由不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13069295/

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