简体   繁体   English

如何在电子角样板中使用$ routeProvider的解析?

[英]How to use $routeProvider's resolve in electron-angular-boilerplate?

I'm having an issue with loading JSON data before entering main controller. 我在进入主控制器之前加载JSON数据时遇到问题。

I'm using this project as a template for my project. 我正在将该项目用作项目的模板。 I basically altered only dist/app/home/home.js and there are the altered contents: 我基本上只更改了dist / app / home / home.js,并且更改了内容:

angular.module('WellJournal', ['uiGmapgoogle-maps', 'ngRoute'])
    .config(function (uiGmapGoogleMapApiProvider, $routeProvider) {
        uiGmapGoogleMapApiProvider.configure({
            libraries: 'geometry,visualization,places'
        });

        $routeProvider.when('/', {
            templateUrl: 'index.html',
            controller: 'MainController',
            resolve:  {
                markers: ['$http', function ($http) {
                    return $http.get('http://address/api/markers/').then(function (result) {
                        console.log(result.data);
                        return result.data;
                    });
                }]
            }
        });

        $routeProvider.otherwise({ redirectTo: '/' });
    })
    .controller('MainController', function ($scope, $uibModal) {
    //trying to access $scope.markers here
    }

And the thing is markers: ['$http', function ($http) {...}] doesn't get triggered. 事情就是markers: ['$http', function ($http) {...}]不会被触发。 So I checked the address of the default page being loaded ( window.location.href ) and it turned out to be file:///home/myuser/path/to/project/dir/views/index.html (you can see the corresponding code here ). 所以我检查了正在加载的默认页面的地址( window.location.href ),结果是file:///home/myuser/path/to/project/dir/views/index.html (您可以看到此处对应的代码)。

So it doesn't really set up a server and just opens local file (I guess?). 因此,它实际上并没有设置服务器,而只是打开本地文件(我猜是吗?)。

How can I get this $routeProvider.when(...) clause triggered? 我如何触发此$routeProvider.when(...)子句? Can I do it at all? 我可以做吗?

Thank you. 谢谢。

If someone else is experiencing the same issue - I end up using next method: 如果其他人遇到相同的问题-我最终会使用下一种方法:

angular.module('WellJournal', ['uiGmapgoogle-maps'])
        .run(function($http, $rootScope){
            $http.get('http://address/api/markers/').
            success(function(data, status, headers, config) {
                $rootScope.markers = data;
                $rootScope.$broadcast('markers-loaded');
            }).
            error(function(data, status, headers, config) {
                // log error
                alert('error');
            });
        })
        .factory('markers', function ($rootScope) {
            var markers;
            $rootScope.$on('markers-loaded', function(){
                markers = $rootScope.markers;
            });
            return {
                data: markers
            }
        })
        .config(function (uiGmapGoogleMapApiProvider) {
            uiGmapGoogleMapApiProvider.configure({
                libraries: 'weather,geometry,visualization,places'
            });
        })
        .controller('MainController', function ($scope, $uibModal, $http, $rootScope, markers) {
        // use data through markers argument
        //Warning! This doesn't assure that the data is loaded before page
        //I'm experiencing small visual lag while loading but this is the best I could do 
        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM