2014-11-03 79 views
0

我正在使用IONIC框架和Angular JS構建一個應用程序,我使用了離子發佈的Tinder卡功能(http://ionicframework.com/blog/tinder-for-x/)。我正在嘗試從JSON文件中讀取並使用此json文件作爲我的數組的存儲。我怎樣才能做到這一點,下面是我的代碼與正常陣列Angular JS,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(); 
}; 
}) 

這是我的代碼試圖使用JSON的方法我得到的錯誤CardsCtrl是從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(); 
}; 
}) 

閱讀不是功能。

+0

你有一個語法錯誤尋找答案。當你只需要關閉這些語句時,有兩個'})''在這裏''處理錯誤''之後''。 – 2014-11-03 15:17:55

+0

啊不能相信我沒有看到。即使使用正確的語法,它仍然不會從我的json文件讀取 – Adam 2014-11-03 15:45:44

回答

3

語法不正確,我有時會忘記自己使用哪一個。 $http.get()返回承諾successerror方法。

$http.get('../cards.json').success(function(cards){ 
    console.log(cards); 
}).error(function(error) { 
    console.log(error); 
});