Angular-种子范围问题
Angular-seed scope issue
我目前正在尝试创建一个在每个 routeProvider 之前运行的 post。目前我收到 $http 未定义的问题,我不知道为什么。我目前正在尝试将 $http 传递给该函数,根据 phpstorm $http 是 'undefined' 并且不知道为什么。我确实在 index.html.
中的 validation.js 之前声明了 angular.js
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="components/universal/validation.js"></script>
我的 app.js 看起来像这样:
'use strict';
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.test',
'myApp.version'
]).
config(['$routeProvider','$http', function($routeProvider,$http) {
alert ('before call');
var temp = sessionValidation($http);
alert('temp : '+temp);
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
Components/universal/validation.js
function sessionValidation($http) {
alert('before post');
return $http({
url: 'http://255.255.255.255/rip.dll/REST/SESSIONS/',
method: 'POST',
dataType:"json",
xhrFields :{"withCredentials" : true},
data: {'logintype':'1','host':'255.255.255.255','user':'Administrator','password':'1234','controlid':'ABC999'}
})
.success(function (data) {
return data.stats;
})
.error(function () {
return 'Error';
});
}
浏览器显示如下:
这是错误信息。
[$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:unpr] Unknown provider: $http http://errors.angularjs.org/1.4.5/$injector/unpr?p0=%24http at Anonymous function (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4284:13) at getService (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4432:11) at invoke (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4461:9) at runInvokeQueue (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4379:11) at Anonymous function (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4388:11) at forEach (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:336:11) at loadModules (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4369:5) at createInjector (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4294:3) at doBootstrap (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1655:5) at bootstrap (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1676:5) http://errors.angularjs.org/1.4.5/$injector/modulerr?p0=myApp&p1=Error%3A%20%5B%24injector%3Aunpr%5D%20Unknown%20provider%3A%20%24http%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.4.5%2F%24injector%2Funpr%3Fp0%3D%2524http%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4284%3A13)%0A%20%20%20at%20getService%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4432%3A11)%0A%20%20%20at%20invoke%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4461%3A9)%0A%20%20%20at%20runInvokeQueue%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4379%3A11)%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4388%3A11)%0A%20%20%20at%20forEach%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A336%3A11)%0A%20%20%20at%20loadModules%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4369%3A5)%0A%20%20%20at%20createInjector%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4294%3A3)%0A%20%20%20at%20doBootstrap%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1655%3A5)%0A%20%20%20at%20bootstrap%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1676%3A5)
任何帮助或指出未设置 $http 的正确方向都会有所帮助。谢谢你。如果您需要任何其他信息,请告诉我,以便我更新问题。
$http
服务无法在 config
阶段使用,因为提供程序可能尚未准备就绪。这里可以使用的是 resolve
属性 in $routeProvider.when()
方法:
'use strict';
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.version'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/view1', {
resolve: {
sessionValidation: function($http) {
return sessionValidation($http);
}
}
})
.otherwise({redirectTo: '/view1'});
}])
我目前正在尝试创建一个在每个 routeProvider 之前运行的 post。目前我收到 $http 未定义的问题,我不知道为什么。我目前正在尝试将 $http 传递给该函数,根据 phpstorm $http 是 'undefined' 并且不知道为什么。我确实在 index.html.
中的 validation.js 之前声明了 angular.js<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="components/universal/validation.js"></script>
我的 app.js 看起来像这样:
'use strict';
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.view2',
'myApp.test',
'myApp.version'
]).
config(['$routeProvider','$http', function($routeProvider,$http) {
alert ('before call');
var temp = sessionValidation($http);
alert('temp : '+temp);
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
Components/universal/validation.js
function sessionValidation($http) {
alert('before post');
return $http({
url: 'http://255.255.255.255/rip.dll/REST/SESSIONS/',
method: 'POST',
dataType:"json",
xhrFields :{"withCredentials" : true},
data: {'logintype':'1','host':'255.255.255.255','user':'Administrator','password':'1234','controlid':'ABC999'}
})
.success(function (data) {
return data.stats;
})
.error(function () {
return 'Error';
});
}
浏览器显示如下:
这是错误信息。
[$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:unpr] Unknown provider: $http http://errors.angularjs.org/1.4.5/$injector/unpr?p0=%24http at Anonymous function (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4284:13) at getService (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4432:11) at invoke (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4461:9) at runInvokeQueue (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4379:11) at Anonymous function (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4388:11) at forEach (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:336:11) at loadModules (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4369:5) at createInjector (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:4294:3) at doBootstrap (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1655:5) at bootstrap (http://localhost:90/angular-seed/app/bower_components/angular/angular.js:1676:5) http://errors.angularjs.org/1.4.5/$injector/modulerr?p0=myApp&p1=Error%3A%20%5B%24injector%3Aunpr%5D%20Unknown%20provider%3A%20%24http%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.4.5%2F%24injector%2Funpr%3Fp0%3D%2524http%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4284%3A13)%0A%20%20%20at%20getService%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4432%3A11)%0A%20%20%20at%20invoke%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4461%3A9)%0A%20%20%20at%20runInvokeQueue%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4379%3A11)%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4388%3A11)%0A%20%20%20at%20forEach%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A336%3A11)%0A%20%20%20at%20loadModules%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4369%3A5)%0A%20%20%20at%20createInjector%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A4294%3A3)%0A%20%20%20at%20doBootstrap%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1655%3A5)%0A%20%20%20at%20bootstrap%20(http%3A%2F%2Flocalhost%3A90%2Fangular-seed%2Fapp%2Fbower_components%2Fangular%2Fangular.js%3A1676%3A5)
任何帮助或指出未设置 $http 的正确方向都会有所帮助。谢谢你。如果您需要任何其他信息,请告诉我,以便我更新问题。
$http
服务无法在 config
阶段使用,因为提供程序可能尚未准备就绪。这里可以使用的是 resolve
属性 in $routeProvider.when()
方法:
'use strict';
angular.module('myApp', [
'ngRoute',
'myApp.view1',
'myApp.version'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/view1', {
resolve: {
sessionValidation: function($http) {
return sessionValidation($http);
}
}
})
.otherwise({redirectTo: '/view1'});
}])