我想要在AngularJs中使用/ example/car/id模板站点,其中不同的ID是不同的汽车页面。 在app.js我写道:角模板页面
angular
.module('carApp', [
'ngCookies',
'ngResource',
'ngRoute',
])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
})
.when('/car/:id', {
templateUrl: 'views/car.html',
controller: 'CarCtrl'
})
});
以JSON我做了2辆:
[{
"model": "Audi",
"year": "1999"
}
{
"model": "BMW",
"year": "2005"
}]
在car.js我需要写有
angular.module('carApp')
.controller('CarCtrl', ['$scope','$http', function($scope, $http)
{
$http.get('scripts/controllers/cars.json').success (function(data){
$scope.cars = data;
});
}]
);
在哪里和什么码example/car/1将显示奥迪和example/car/2的详细信息的模板将显示有关BMW的详细信息?
您应该包括在JSON的ID为每一辆汽车,如果你想在url中使用id。否则,我会建议使用模型。要去页面jsut使用链接 krs8785 2014-11-05 15:57:20
,然后在html中,我怎么能得到它? 汽车[{{id}}]。name和angular会知道如果它的example/car/1的id是1并且从第一个对象读取细节? 或者我需要做一些更多的魔法? – 2014-11-05 16:29:34