2014-11-03 2 views
0

Я создаю приложение с использованием IONIC-рамок и углового JS, я использую функцию Tinder cards, которую ионный выпустил (http://ionicframework.com/blog/tinder-for-x/). Я пытаюсь прочитать из файла JSON и использовать этот json-файл в качестве хранилища для моего массива. Как я могу это сделать, ниже мой код с нормальным массивомУгловое 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 файла

//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(); 
}; 
}) 

При использовании метода JSON я получаю ошибку CardsCtrl является не является функцией.

+0

У вас есть ошибка синтаксиса. Есть два '}):' после '// обрабатывать ошибку здесь ', когда вам нужен только один для закрытия операторов. –

+0

ahh can not считаю, что я этого не видел. Даже с правильным синтаксисом он все еще не читает из моего json-файла – Adam

ответ

3

Синтаксис неправильный, я иногда забываю себя, который использовать. $http.get() возвращает обещание с методами success и error.

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