如何使用Ionic / Angular JS带有phonegap的工厂? [英] How to use factories with Ionic/Angular JS with phonegap?

查看:107
本文介绍了如何使用Ionic / Angular JS带有phonegap的工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用工厂服务在我的ionic / angular + phonegap应用程序项目中使用全局变量。

I'm trying use global variables in my ionic/angular + phonegap app project by using factory service.

但是,添加一个简单的工厂服务就像一个下面以某种方式混淆了应用程序&应用程序的所有屏幕都变为纯白色。

However, adding even a simple factory service like the one below messes up the app somehow & all screens of the app become pure white.

.factory('serviceName', function() {
    return {}
})

我有两个名为 app的js文件.js & controller.js

I've 2 js files named app.js & controller.js

app.js看起来像这样有更多的状态:

app.js looks like this with much more states:

// 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 有点像这样有更多的变量&函数:

My controller.js somewhat looks like this with much more variables & functions:

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/ 开始使用工厂。

I referred this http://mcgivery.com/ionic-using-factories-and-web-services-for-dynamic-data/ to start using factories.

我知道可以使用$ rootScope,但它拥有所有全局变量的问题,所以请帮我使用工厂。此外,如果有人可以给我调试提示与phonegap。我在android上使用phonegap开发应用程序,并在使用它时一直关注控制台。

I know can use $rootScope but it has all the problems of global variables, so please help me use factories. Also, if someone can give me debugging tips with phonegap. I use the phonegap development app on android and keep an eye on the console while working with it.

推荐答案

你可以使用在您的应用中出厂 ,如下所示:

You can use factory in your app like this:

.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;
        }
    };
})

并拨打 控制器

and call in your controller

.controller('PlaylistsCtrl', function($scope, serviceName) { 
    $scope.playlists =  serviceName.myPlayList();
    $scope.myvariablevalue =  serviceName.getValue();    
})

这篇关于如何使用Ionic / Angular JS带有phonegap的工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆