javascript Angular JS,从 json 中读取离子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26714928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular JS, ionic reading from json
提问by Adam
I am building an App using IONIC framework and Angular JS, I am using the Tinder cards feature which ionic have released (http://ionicframework.com/blog/tinder-for-x/). I am trying to read from a JSON File and use this json file as storage for my array. How can i do this, below is my code with a normal array
我正在使用 IONIC 框架和 Angular JS 构建一个应用程序,我正在使用 ionic 发布的 Tinder 卡功能(http://ionicframework.com/blog/tinder-for-x/)。我正在尝试从 JSON 文件中读取数据并将此 json 文件用作阵列的存储。我该怎么做,下面是我的代码与普通数组
//Controller for Cards
angular.module('Pinder').controller('CardsCtrl', function($scope, TDCardDelegate) {
console.log('CARDS CTRL');
//Array of Cards - this should be moved to a service file.
var cardTypes = [
{id: 1, title: "Frank", image: 'img/Frank.png', desc:"This will be card Description", done: false },
{id: 2, title: "John Lewis", image: 'img/JohnLewis.png', desc:"This will be card Description", done: true },
{id: 3, title: "Generali", image: 'img/Generali.png', desc:"This will be card Description", done: true },
];
//Calls CardTypes array into 'cards'
$scope.cards = Array.prototype.slice.call(cardTypes, 0);
$scope.cardDestroyed = function(index) {
$scope.cards.splice(index, 1);
};
//Randomly adds a new card
$scope.addCard = function() {
var newCard = cardTypes[Math.floor(Math.random() * cardTypes.length)];
newCard.id = Math.random();
$scope.cards.push(angular.extend({}, newCard));
}
})
//Controller for swipe interface of the card.
.controller('CardCtrl', function($scope, TDCardDelegate) {
//Card swipe left function
$scope.cardSwipedLeft = function(index) {
console.log('LEFT SWIPE');
$scope.addCard();
};
//Card swipe right function
$scope.cardSwipedRight = function(index) {
console.log('RIGHT SWIPE');
$scope.cards[index].done = true;
$scope.addCard();
};
})
This is my code attempting to read from a json file
这是我尝试从 json 文件中读取的代码
//Controller for Cards
angular.module('Pinder').controller('CardsCtrl', function($scope, TDCardDelegate, $http) {
console.log('CARDS CTRL');
$scope.cards = [];
$http.get('../cards.json', function(cardTypes){
//Calls CardTypes array into 'cards'
$scope.cards = Array.prototype.slice.call(cardTypes, 0);
}, function(error) {
//handle error here
});
});
$scope.cardDestroyed = function(index) {
$scope.cards.splice(index, 1);
};
//Randomly adds a new card
$scope.addCard = function() {
var newCard = cardTypes[Math.floor(Math.random() * cardTypes.length)];
newCard.id = Math.random();
$scope.cards.push(angular.extend({}, newCard));
}
})
//Controller for swipe interface of the card.
.controller('CardCtrl', function($scope, TDCardDelegate) {
//Card swipe left function
$scope.cardSwipedLeft = function(index) {
console.log('LEFT SWIPE');
$scope.addCard();
};
//Card swipe right function
$scope.cardSwipedRight = function(index) {
console.log('RIGHT SWIPE');
$scope.cards[index].done = true;
$scope.addCard();
};
})
When using the json method i get the error CardsCtrl is not a function.
使用 json 方法时,我收到错误 CardsCtrl is not a function。
回答by Jeremy Wilken
The syntax is incorrect, I sometimes forget myself which to use. $http.get()
returns a promise with a success
and error
methods.
语法不正确,我有时会忘记使用哪个。$http.get()
返回带有 asuccess
和error
方法的承诺。
$http.get('../cards.json').success(function(cards){
console.log(cards);
}).error(function(error) {
console.log(error);
});