AngularJs .Run没有运行

时间:2018-04-04 09:28:35

标签: javascript angularjs node.js

我认为模块没有运行。在我的index.js中,我有

// index.js ---type 1
// .run not working...
var myServiceToRun = require('./myservice.js');
var mod = angular.module('myApp',[]);
mod.run(myServiceToRun);

module.exports = mod;
// result is only printing console 1... mostly because of the require

// index.js --- type 2
// not working too
var myServiceToRun = require('./myservice.js');
var mod = angular.module('myApp',[]);

module.exports = mod;
// result is only printing console 1... mostly because of the require

在myservice.js文件中:

// myservice.js -- type 1
// not working
'use strict';
(function() {
    var mod = angular.module('myApp',[]);
    console.log("In my service 1");

    mod.run(function ($rootScope,$http, $state, $window, $location, urlService, $cookies,accountSecurityServices, localStorageService) {
        console.log("In my service 2");
    });
})();

它永远不会进入第二个控制台日志。 我也尝试过这个片段,但它没有用。

//myservice.js --- type 2
// not working
console.log("In my service 1");
module.exports = function ($rootScope,$http, $state, $window, $location, urlService, $cookies,accountSecurityServices, localStorageService) {
    console.log("In my service 2");
    // i will put some stateChange/transition here later on...
}}

我也试过在index.js文件中取出.run,但它也没有用。

除此之外,我试图删除对myservice.js文件的依赖,并希望它能够正常工作,但它也不起作用......

//index.js
// not working too 

var otherService = require('./otherservice.js');

var mod = angular.module('myApp',[]);

mode.factory('otherService',['$http', otherService]);
mod.run(function ($rootScope, $http, $state, $window, $location, urlService, $cookies, localStorageService) {
    console.log("In my service 2");
});

module.exports = mod;
// my other service, is working fine... just that the .run is not running...

我相信这应该是一项非常简单的任务,但我无法做到正确......任何想法?

该应用程序的应用程序的其他部分工作正常,我的其他控制器/服务正在工作,但它从未进入运行功能...它将始终显示控制台1,我认为打印输出因为要求声明......

1 个答案:

答案 0 :(得分:0)

您的运行块中似乎存在语法错误,同样在声明模块时也是如此。

'use strict';

(function() {
    var mod = angular.module('myApp', []);
    console.log("In my service 1");

    mod.run(function ($rootScope, $http, $state, $window, $location, urlService, $cookies, accountSecurityServices, localStorageService) {
        console.log("In my service 2");
    });
})();

您的myServiceToRun声明中还有另一个语法错误

var myServiceToRun = require('./myservice.js');