在Angular中传递承诺解决

时间:2017-11-15 12:43:00

标签: angularjs promise angular-ui-router resolve

所以我试图将承诺从http请求传递给一个解决方案,然后只有在收到加载视图时才会这样做。我想知道错误在哪里,因为我得到一个未捕获的错误:[$ injector:modulerr]错误。 "主要搜索" controller是定义state.go的控制器,而不是模板的控制器。我不确定这是否会导致问题。

//in app.js
  .state({
                name: "mainSearch",
                url: "/mainSearch",
                views: {
                    'testView': {
                        template: '<div><span>{{params}} , I load after the promise</span></div>',
                        controller: "mainSearch",
                    }
                },
                resolve: {
                    entityParams: ['$stateParams', 'mainSearch', function ($stateParams) {        console.log("I load before the view as i contain a promise")
                        return $stateParams
                    }]
                }

            })

    // in mainSearch.js 
    state.go("mainSearch", {
            params: httpfactory.getResult('GET', param)
          })


// in http-factory module
    var app = angular.module("http-factory", []);

    app.factory('httpfactory', ['$http', function ($http) {

        $scope.getResult = function (callType, param) {
            switch (callType) {
                case 'POST':
                    $http.post('/abcurl', 'requestBody', {
                        withCredentials: true,
                        headers: {
                            "abc": "abc"
                        }
                    })
                    break;

                case 'GET':
                    return $http.get('/gttportal/api/search', {
                        params: param,
                        withCredentials: true
                    })

            }
            break;
        }
    }]);

这就是我的app.js和模块配置的方式

app = angular.module('myapp', ['http-factory','mainsearch']
app.config(function ($stateProvider, $urlRouterProvider, httpfactory) {...

这就是我配置mainsearch模块和功能的方法:

 .module('mainsearch', ['http-factory'])

function ($rootScope, $scope, $http, $state,  $stateParams, httpfactory)

0 个答案:

没有答案