【发布时间】:2016-05-10 10:19:53
【问题描述】:
我正在尝试显示我的 json 文件中的数据,如下所示:
[
{
"category":"category1",
"link":"category1",
"expand":false,
"keyword":"category1, category1 online, category1 something"
},
{
"category":"category2",
"link":"category2",
"expand":false,
"keyword":"category2, category2 online, category2 something"
}
]
我还在控制器中使用 $routeProvider:
var myApp = angular.module('myApp', [
'ngRoute',
'MyControllers'
]);
myApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/:itemName', {templateUrl: function(param){
return'partials/'+ param.itemName +'.html';
}, controller: 'ContentCtrl'});
$routeProvider.otherwise({redirectTo: '/category1'});
}]);
var MyControllers = angular.module('MyControllers', ['ngAnimate']);
MyControllers.controller('ContentCtrl', ['$scope', '$http', '$routeParams', function($scope, $http, $routeParams) {
$http.get('js/data.json').success(function(data){
$scope.meta = data;
$scope.whichItem = $routeParams.itemName.indexOf()+1;
});
}]);
最后是模板:
<html ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<section ng-view>
<h1>{{meta[whichItem].h1}}</h1>
</section>
</body>
</html>
所以问题是参数$whichItem 总是返回0 作为值。例如,当我将路由更改为 /category2 并显示来自 json 数据的相同关键字时,它不想重新加载。这有什么问题?
【问题讨论】:
-
String.prototype.indexOf() indefOf('What?") 在 IndexOf 方法中添加您要查找的内容
-
好的。我正在尝试这个: $scope.whichItem = $routeParams.itemName.indexOf()+1;因为 routeparam 的正常值不起作用: $scope.whichItem = $routeParams.itemName;
-
有效的是:{{meta[0].h1}} 和 {{meta[1].h1}} 然后我从 category1 和 category2 中获取关键字。 {{meta[whichItem].h1}} 不起作用!
标签: javascript arrays angularjs json ngroute