angularjs + requirejs + dependency in resolve

前端 未结 1 1104
粉色の甜心
粉色の甜心 2021-01-16 00:23

How I can correct inject dependency P48Wallet in resolve for $routeProvider?

app.js

\'use strict\';

define(
[
    \'angularAMD\',
          


        
1条回答
  •  被撕碎了的回忆
    2021-01-16 01:17

    2 problems that I can see:

    1. angularAMD.route sets the resolve property so it is overwriting the resolve that you are setting up.
    2. return app.factory expect an object to be return and not this

    What you should be doing is define the P48Wallet as a factory/service and then add it to the dependency to your Main controller. For example:

    application/services/P48Wallet.js

    EDIT: Adding feature to cache the returned data from POST per comment.

    define(['application/app', 'application/services/Http'], function(app) {
        app.register.service('P48Wallet', function(Http, $q) {
            var priv_data;
    
            // Assuming that your Http works the same way as $http
            this.getUserData = function() {
                var d = $q.defer();
    
                if (priv_data) {
                    // Return cached value
                    d.resolve(priv_data);
                } else {
                    Http.post('?action=get_data')
                    .then(function (data) {
                        priv_data = data;
                        d.resolve(data);
                    })
                    .catch(function (error) {
                        d.reject(error);
                    });
                }
            };
    
            return d.promise;
        });
    });
    

    application/controllers/Main.js

    define(['application/app', 'application/services/P48Wallet'], function (app) {
        app.register.controller("MainController", [
            '$scope', 'P48Wallet', function ($scope, P48Wallet) { ... }
        ]);
    });
    

    application/app.js

    // Keeping everything the same except
    angularAMD.route({
        templateUrl: 'static/js/application/views/main.html',
        controllerUrl: 'application/controllers/Main',
        controller: 'MainController'
    })
    

    Here is angularAMD's documentation on how to setup the route to lazy load controllers:

    https://github.com/marcoslin/angularAMD#on-demand-loading-of-controllers

    0 讨论(0)
提交回复
热议问题