JSON對象我有JSON這樣得到基於
[
{
"id":1,
"name": "BMW E46 PANDEM",
"price": 130000,
"curency":"USD",
"color":"white",
"category":"car"
},
{
"id":2,
"name": "Yamaha",
"price": 2000,
"curency":"USD",
"color":"white",
"category":"Bike",
},
{
"id":3,
"name": "Nissan GTR R34",
"price": 130000,
"curency":"USD",
"color":"black",
"category":"car"
}
]
廠這樣
angular
.module('app')
.factory('ItemFactory', ['$http', function($http)
{
return {
get: function() {
return $http.get('scripts/item.json').then(function(response) {
return response.data;
});
}
};
}
]);
控制器
angular
.module('app')
.controller('apparelCtrl',['$scope','ItemFactory', function($scope,ItemFactory){
$scope.title='car';
ItemFactory.get().then(function(data) {
$scope.apparelItems = data;
});
$scope.apparelItems=ItemFactory.get();
console.log($scope.apparelItems);
}]);
directive.js
angular
.module('app.directives.gridViewApparel',[])
.directive('gridApparel',function()
{
return{
restrict:'E',
scope:{
data: '='
},
transclude:true,
replace:true,
templateUrl:"templates/directives/gridViewApparel.html",
controller:function($scope){
console.log($scope.data);
}
};
}
);
diirective.html
<div>
<div class="col-sm-4 ApparelThumbnail">
<p class="title">{{data.name}}</p>
<p>{{data.price}}{{curency}}</p>
<a href="#/Apparel/{{data.id}}" class="btn btn-primary">detail</a>
</div>
</div>
我car.html
<div class="container">
<h1>{{title}}</h1>
<div ng-repeat="carlItem in carItems | filter: {category:'car'}" class="">
<grid-view-apparel data="carlItem"></grid-view-apparel>
</div>
</div>
我的兩個car.html和bike.html遇到錯誤
angular.js:14525 Error: [filter:notarray]
我嘗試通過指數前添加軌道過濾但仍然有同樣的錯誤。 如何從基於類別傢伙的json獲取對象? 謝謝
錯誤可能是由'網格視圖apparel'引起的。也許你可以對你正在使用的指令'grid-view-apparel'做一些額外的解釋。 – Pengyy
@Pengyy是的先生我會更新我的代碼 – GerryofTrivia
也''gridViewApparel.html'的後代碼。 – Pengyy