【问题标题】:Simple Ionic App Not Rendering When Adding a Factory to the Controller将工厂添加到控制器时,简单的 Ionic 应用程序不渲染
【发布时间】:2015-10-18 00:52:41
【问题描述】:

我有一个演示 ionic 应用程序,用于从 joshmorony.com 进行映射。当我将工厂引入控制器时,它会完全停止渲染。以下用于显示地图,但如果我将控制器线更改为标记为 FAIL 的线,它不会通过模拟器呈现。任何想法为什么?我在 Visual Studio 2015 中通过 Ripple 模拟器运行它。

// 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'
angular.module('starter', ['ionic', 'ngCordova'])

.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);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
    })
})

.config(function ($stateProvider, $urlRouterProvider) {

    $stateProvider
    .state('map', {
        url: '/',
        templateUrl: 'templates/map.html',
        controller: 'MapCtrl'
    });

    $urlRouterProvider.otherwise("/");

})


.factory('JMGoogleMaps', function($cordovaGeolocation) {

    function initMap(){
        
    };

})

**// FAILS to render at all...
// .controller('MapCtrl', function ($scope, $state, $cordovaGeolocation, JMGoogleMaps) {**
.controller('MapCtrl', function ($scope, $state, $cordovaGeolocation) {

    var options = { timeout: 10000, enableHighAccuracy: true };

    $cordovaGeolocation.getCurrentPosition(options).then(function (position) {

        var latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

        var mapOptions = {
            center: latLng,
            zoom: 15,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        $scope.map = new google.maps.Map(document.getElementById("map"), mapOptions);

        //Wait until the map is loaded
        google.maps.event.addListenerOnce($scope.map, 'idle', function () {

            var marker = new google.maps.Marker({
                map: $scope.map,
                animation: google.maps.Animation.DROP,
                position: latLng
            });

            var infoWindow = new google.maps.InfoWindow({
                content: "Here I am!"
            });

            google.maps.event.addListener(marker, 'click', function () {
                infoWindow.open($scope.map, marker);
            });

        });

    }, function (error) {
        console.log("Could not get location");
    });
});

【问题讨论】:

    标签: angularjs ionic factory


    【解决方案1】:

    看来工厂要退货了。

    我改成:

    .factory('JMGoogleMaps', function ($cordovaGeolocation) {
    
    var fac = {};
    
    fac.users = ['John', 'James', 'Jake']; 
    
    return fac;
    

    })

    基于http://viralpatel.net/blogs/angularjs-service-factory-tutorial/,然后我可以取消注释 FAIL 行并且它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 2017-10-25
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多