使用ng-file-upload时,工厂未定义

时间:2016-09-22 07:27:39

标签: javascript angularjs ng-file-upload angular-module

我有一个表单,我保存了一些细节并上传了一个文件。我有一个工厂,我可以从中获得一些数据。当我使用ng-file-upload时,工厂未定义。这是代码:

      angular.module('tollApp')
     .controller('mstrEmployeeCtrl',['Upload','$window',
       function(Upload,$window,$scope,$http,$timeout,$filter,$mdToast,userDetailsFactory){

       $scope.ipForHttp = userDetailsFactory.getUserDetailsFromFactory().ipAddress;
    //`userDetailsFactory` is undefined
       $scope.SaveData = function(){
            // $scope.dobObj = new Date($scope.Emp.EmpDoB);
             if ($scope.myform.$valid && $scope.passwordEqual) {
            Upload.upload({
                url: '$scope.ipForHttp+"addEmployee?EmpID="+$scope.Emp.EmpID,

                data:{file:file} //pass file as data, should be user ng-model
            })
            .then(function(response){
                $scope.error=response.data.code;
                console.log(JSON.stringify(response));
               console.log($scope.error+" SCOPE");

            })
                $scope.submitted = false;
            }
        };

}
]);

错误:

angular.min.js:117 TypeError: Cannot read property 'getUserDetailsFromFactory' of undefined
at new <anonymous> (http://192.168.1.19/public/javascripts/mstrEmployeeCtrl.js:4:38)
at Object.instantiate (http://192.168.1.19/public/javascripts/angular.min.js:41:477)
at http://192.168.1.19/public/javascripts/angular.min.js:90:3
at Object.link (http://192.168.1.19/node_modules/angular-route/angular-route.min.js:7:274)
at http://192.168.1.19/public/javascripts/angular.min.js:16:230
at ia (http://192.168.1.19/public/javascripts/angular.min.js:81:35)
at n (http://192.168.1.19/public/javascripts/angular.min.js:66:176)
at g (http://192.168.1.19/public/javascripts/angular.min.js:58:429)
at http://192.168.1.19/public/javascripts/angular.min.js:58:67
at http://192.168.1.19/public/javascripts/angular.min.js:62:430 <div data-ng-view="" class="ng-scope" data-ng-animate="1">

未定义的工厂:

    angular.module('tollApp')
.controller('indexController', function($scope,$http,$window,userDetailsFactory){
    $scope.usernameFromServer={};
    $scope.getUserDetails = function(){
        $http({
            method:'GET',
            url:'http://192.168.1.19:80/getUserDetails'
        })
        .then(function(response){

            // console.log(JSON.stringify(response));
            userDetailsFactory.setUserDetailsInFactory(response.data);
        $scope.usernameFromFactory = userDetailsFactory.getUserDetailsFromFactory().usernameFromSession;

            // $scope.usernameFromServer = userDetailsFactory.getUserDetailsFromFactory().username;
            // console.log(JSON.stringify($scope.usernameFromFactory)+"usernameFromFactory");
        })
    }
    $scope.logout = function(request,response){
        $http({
            method:'GET',
            url:'/logout'
        })
        .then(function(response){
            console.log(JSON.stringify(response));
            if(response.data=="logout"){
                $window.location.href="http://192.168.1.19:80/login";
            }
        })
    }
    console.log("indexController");
}).factory('userDetailsFactory',function(){
    var user = {};
    return {
        setUserDetailsInFactory : function(val){
            user.useridFromSession = val[0].UserID;
            user.usernameFromSession = val[0].UserName;
            user.userroleFromSession = val[0].UserRole;
            user.clientidFromSession = val[0].ClientID;
             user.ipAddress = "http://192.168.1.19:80/";
            // user.ipAddress = "http://easypaytoll.com/";
            // console.log("in set "+user.clientidFromSession);
        },
        getUserDetailsFromFactory : function(){
            return user;
        }
    };
});

1 个答案:

答案 0 :(得分:0)

这是注射问题。 您必须将array_keys($obj->getFields()) 放在数组中:

userDetailsFactory

修改1 您的错误很明确:未定义angular.module('tollApp') .controller('mstrEmployeeCtrl',['Upload','$window','$scope', '$http','$timeout','$filter','$mdToast','userDetailsFactory' function(Upload,$window,$scope,$http,$timeout,$filter,$mdToast,userDetailsFactory){ 。你是怎么定义这个工厂的?它在同一个模块中吗?如果没有,你是否将其模块添加到应用程序的模块依赖项?