我正在使用MEAN堆栈。对于我的前端,我试图用角度呈现项目列表中单个项目的数据。当我点击该项目时,网址会更改为我设置的以外的值,并且我不会收到这些数据。我只能在使用调试模式后才能看到它。预期链接是/item/:itemid
但我正在逐渐/item/partials/item.html
我已经使用了以下文件:angularJs中的url链接不正确
main.route.js(只显示问题的途径)
function config($routeProvider, $locationProvider) {
$routeProvider
.when('/item/:itemid', {
templateUrl: 'partials/item.html',
controller: 'productCtrl',
controllerAs: 'vm'
})
.otherwise({redirectTo: '/'});
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
};
myData.service.js
angular
.module('myApp')
.service('myData', myData);
function myData ($http) {
var itemById = function (itemid) {
return $http.get('/api/items/' + itemid);
};
return {
itemById : itemById
};
}
product.controller.js
angular
.module('myApp')
.controller('productCtrl', productCtrl)
function productCtrl($routeParams, myData) {
var vm = this;
vm.itemid = $routeParams.itemid;
myData.itemById(vm.itemid)
.success(function(data) {
vm.data = { item : data };
vm.pageHeader = {
title: vm.data.item.name
};
})
.error(function (e) {
console.log(e);
});
};
items.html(链接到单个项目页)
<a href='/#/item/{{ item._id }}'>{{ item.name }}</a>
item.html
<div ng-controller='productCtrl'>
<div class='container'>
<page-header content='vm.pageHeader.title'></page-header>
</div>
</div>
原谅我,如果这个问题没有太大的意义,因为我这是新的。提前致谢!
这就是为什么角建议您设置了'' 使用时HTML5模式。您的'templateUrl'正在相对于当前URL路径加载,该路径是'/ item' –
Phil
另外,不要在路由中使用'ng-controller'。您已经在路线配置中设置了控制器 – Phil
非常感谢。它的工作 –