AngularJs .Run not running



我认为模块未运行。在我的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,我相信由于要求而被打印出来语句...

您的运行块中似乎有一个语法错误,同样在声明模块中。

'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');

最新更新