将控制器与组件分开

时间:2017-08-31 08:13:10

标签: angularjs

组件: crudModule.js

var crudModule = angular.module('crudModule', ['ui.router', 'smart-table', 'ngCookies', 'ui.bootstrap', 'angularModalService', 'dialogs', 'remoteValidation']);

angular.module('crudModule').component('applicationInfo', {
    templateUrl: 'infoApplication.html',
    controller: 'applicationInfoCtrl'
});

applicationInfoCtrl.js:

var crudModule = angular.module('crudModule')

crudModule.controller('applicationInfoCtrl', ['httpService', '$scope', function($http, $scope, $cookies, $stateParams, httpService) {

httpService.httpGetRequest("http://localhost:8080/applications/" + $stateParams.id).then(function success(response) {
    $scope.application = response.data;
});

$scope.getApiKey = function () {
    httpService.httpGetRequest('http://localhost:8080/applications/generateApiKey').then(function success(response) {
        $scope.application.apikey = response.data.apikey;
        $scope.application.apisecret = response.data.apisecret
    })
};
$scope.send = function (object, url) {
    httpService.httpPostRequest(object, url + "/" + $stateParams.id).catch(function(error) {
        console.log('There has been a problem with your fetch operation: ' + error.message);
    }).then(function success(response){
    });
}
}]);

httpService.js:

var crudModule = angular.module('crudModule')


crudModule.factory('httpService', function($http) {
    return {
        httpGetRequest: function (url) {
            return $http({
                method: 'GET',
                url: url
            })
        },
        httpPostRequest: function (object, url){
            return $http({
                method:'POST',
                url: url,
                data: object
            })
        }
    }
});

我收到错误: 无法阅读属性' httpGetRequest'未定义的。

我注入了我的httpService,但我还没有发现任何错误

1 个答案:

答案 0 :(得分:0)

问题是控制器中参数的顺序,它应该是

crudModule.controller('applicationInfoCtrl', ['$http','httpService', '$scope','$cookies','$stateParams' function(http,httpService, $scope,$cookies,$stateParams) {
}