webpack angularjs函数返回字符串而不是对象

时间:2018-01-08 16:58:19

标签: javascript php angularjs webpack

我正在使用webpack创建网页,我遇到了奇怪的错误。

  

数据必须是有效的JSON对象。收到:“......未定义   index:id

所以,我检查了我发送的对象。然后,当我获取ID的服务在控制台日志中返回这样的内容时:

ƒ (){
        return id;
    }

所以,我知道我先运行函数,然后运行html模板。如果我们谈论webpack,我可以在哪里得到错误?

这是我的app.js,我用控制器配置服务和模板:

const css = require('./src/style/app.scss');
var angular = require('angular'); 
var ngRoute = require('angular-route');

var ngModule = angular.module('app', ['ngRoute']);  
require('./src/js/contact')(ngModule);
require('./src/js/adminNetworks')(ngModule);
require('./src/js/automatic')(ngModule);
require('./src/js/login')(ngModule);
require('./src/js/program')(ngModule);
require('./src/js/register')(ngModule);
require('./src/js/main')(ngModule); 


ngModule.service('user', function(){
    var username;
    var loggedin = false;
    var id;

    this.getName = function(){
        return username;
    };

    this.setID = function(userID){
        id = userID;
    };
    this.getID = function(){
        return id;
    };

    this.isUserLoggedIn = function(){
        if(!!localStorage.getItem('login')){
            loggedin = true;
            var data = JSON.parse(localStorage.getItem('login'));
            username = data.username;
            id = data.id;
        }
        return loggedin;
    };

    this.saveData = function(data){
        username = data.user;
        id = data.id;
        loggedin = true;
        localStorage.setItem('login', JSON.stringify({
            username: username,
            id: id
        }));
    };

    this.clearData = function(){
        localStorage.removeItem('login');
        username="";
        id="";
        loggedin = false;
    }

})

ngModule.controller('IndexController', ['$scope', 'user', function ($scope, user) {        
    $scope.aa = function(){
        console.log(user.get);
    }
}])


ngModule.config(function($routeProvider, $locationProvider){ 
    $locationProvider.hashPrefix(''); 
    $routeProvider

    .when('/', {
        templateUrl: 'main.html',
        controller: 'MainCtrl'
    })
    .when('/program', {
        templateUrl: 'program.html',
        controller: 'ProgramCtrl',
        resolve: {
            check: function($location, user){
                if(!user.isUserLoggedIn()){
                    $location.path('/');
                    alert('Musisz być zalogowany')
                }
            }
        }
    })
    .when('/administration', {
        templateUrl: 'adminNetworks.html',
        controller: 'NetworkCtrl'
    })
    .when('/automatic', {
        templateUrl: 'automatic.html',
        controller: 'AutomaticCtrl'
    })
    .when('/contact', {
        templateUrl: 'contact.html',
        controller: 'ContactCtrl'
    })
    .when('/registry', {
        templateUrl: 'register.html',
        controller: 'RegisterCtrl'
    })
    .when('/login', {
        templateUrl: 'login.html',
        controller: 'LoginCtrl'
    })
    .when('/logout', {
        resolve: {
            deadResolve: function($location, user){
                user.clearData();
                $location.path('/');
            }
        }
    })
    .otherwise({
        redirectTo: "/"
      });
});

首先,我想,这是因为像这样的加载块:

 plugins: [
        new HtmlWebpackPlugin({
                title: 'Główna',
                hash: true, 
                allChunks: true,
                template: './src/html/index.html'
        }),
        new HtmlWebpackPlugin({
            title: 'Main',
            hash: true,
            chunks: ['main'],
            filename: 'main.html',
            template: './src/html/main.html'
        }),

但是当我删除块部分时,它仍然是相同的。 请至少提出任何想法。

@Edit - 我的新通行证发件人:

module.exports = function(ngModule) {
    ngModule.controller('MainCtrl', ['$scope', '$http','user', function ($scope, $http, user) {  
               $scope.user = user.getName();
        console.log("LOCAL STORAGE");
        console.log(localStorage);


        $scope.newPass = function(){
            debugger;
            var password = $scope.newpassword;
            $http({
                url: 'http://localhost/webpack/updatePass.php',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: 'newPass='+password+'&id='+user.getID()                  
            }).then(function(response){
                if(response.data.status =='done') {
                    alert('Changed');
                }else {
                    alert('error');
                }      
            })
        }
    }])
}

0 个答案:

没有答案