0
我正在試驗離子(和建設使用phonegap在線建設者),我想現在添加一個工廠,但它弄亂了,並給出一個空白的白色屏幕。我試圖解決類似的問題:How to use factories with Ionic/Angular JS with phonegap?如何添加離子工廠?
但它仍然無法正常工作。我services.js文件看起來像這樣:
.factory('userFactory', [function() {
var users = [
{name: 'Omar', city: 'rizal'},
{name: 'Ganny', city: 'makati'},
{name: 'Chester', city: 'manila'}
];
return {
getUsers: function(){
return users;
},
getUser: function(id){
for(i=0;i<users.length;i++){
if(users[i].name == id)
{
return users[i];
}
}
return null;
}
};
}])
和我的控制器看起來像這樣:
angular.module('app.controllers', [])
.controller('CityCtrl', ['$scope', '$stateParams', '$location'
function ($scope, $stateParams, $location, userFactory) {
$scope.userdata = {};
$scope.enterCity = function(usern,city) {
if(userFactory.getUser(usern) != null)
{
$location.path('/page14');
}
else
{
alert('Failed');
}
}
}])
您'users'數組沒有任何具有'Username'屬性的對象,所以'getUser()'是**總是**會返回'null'。 – Claies
謝謝,我沒有看到。 –