[英]How to use factories with Ionic/Angular JS with phonegap?
我正在嘗試通過使用工廠服務在Ionic / Angular + Phonegap應用程序項目中使用全局變量。
但是,即使添加了以下簡單的工廠服務,該應用程序也會以某種方式混亂,並且該應用程序的所有屏幕都變成純白色。
.factory('serviceName', function() {
return {}
})
我有2個名為app.js
和controller.js
js文件
app.js看起來像這樣,狀態更多:
// Ionic Starter App
// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers'])
//TRIED ADDING FACTORY HERE
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
})
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
});
$urlRouterProvider.otherwise('/app/playlists');
});
我的controller.js
看起來有點像這樣,其中包含更多的變量和函數:
angular.module('starter.controllers', [])
//ALSO TRIED ADDING FACTORY HERE
.controller('AppCtrl', function($scope, $ionicModal, $timeout,$http) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//$scope.$on('$ionicView.enter', function(e) {
//});
// Form data for the login modal
$scope.loginData = {};
})
.controller('PlaylistsCtrl', function($scope) {
$scope.playlists = [
{ title: 'Reggae', id: 1 },
{ title: 'Chill', id: 2 },
{ title: 'Dubstep', id: 3 },
{ title: 'Indie', id: 4 },
{ title: 'Rap', id: 5 },
{ title: 'Cowbell', id: 6 }
];
})
.controller('PlaylistCtrl', function($scope, $stateParams) {
})
我將此http://mcgivery.com/ionic-using-factories-and-web-services-for-dynamic-data/提到來開始使用工廠。
我知道可以使用$ rootScope,但是它具有全局變量的所有問題 ,所以請幫助我使用工廠。 另外,如果有人可以通過phonegap給我調試提示。 我在android上使用phonegap開發應用程序,並在使用它時關注控制台。
您可以像這樣在應用程序中使用工廠 :
.factory("serviceName", function() {
var playlists = [{
title: 'Reggae',
id: 1
}, {
title: 'Chill',
id: 2
}, {
title: 'Dubstep',
id: 3
}, {
title: 'Indie',
id: 4
}, {
title: 'Rap',
id: 5
}, {
title: 'Cowbell',
id: 6
}];
var _myVariableValue = 123;
return {
myPlayList: function() {
return playlists;
},
getValue: function() {
return _myVariableValue;
}
};
})
並致電您的控制器
.controller('PlaylistsCtrl', function($scope, serviceName) {
$scope.playlists = serviceName.myPlayList();
$scope.myvariablevalue = serviceName.getValue();
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.