Javascript AngularJS-未定义服务函数中有错误,但控制器中没有错误-引用错误:未定义systemService
我对AngularJS真的很陌生,所以这可能真的很明显 我有一个服务,它做一些基本的检查,然后返回信息。我在控制器和函数中调用此服务 当我在控制器中调用它时,它工作正常,没有错误 当我在函数中调用它时,我得到 angular.js:9101 ReferenceError:systemService未定义 在工作的控制器中调用服务:Javascript AngularJS-未定义服务函数中有错误,但控制器中没有错误-引用错误:未定义systemService,javascript,angularjs,Javascript,Angularjs,我对AngularJS真的很陌生,所以这可能真的很明显 我有一个服务,它做一些基本的检查,然后返回信息。我在控制器和函数中调用此服务 当我在控制器中调用它时,它工作正常,没有错误 当我在函数中调用它时,我得到 angular.js:9101 ReferenceError:systemService未定义 在工作的控制器中调用服务: myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', fu
myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
$scope.ContinueAngularMethod = function () {
$rootScope.field = 'payment';
$scope.field = $rootScope.field;
$scope.notApp = '1';
console.log("I don't error in here");
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
$rootScope.$apply();
}
}]);
function MyCtrl($scope) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
<script type='text/javascript'>
//Angular stuff
var myApp = angular.module('myApp', []);
//Set up my service
myApp.service('systemService', function () {
this.info = {}; //Declaring the object
this.checkDropDownValue = function (type, notAppJ) {
if (type != 'PayPal') {
console.log("I am not PayPal");
field = 'other'
notApp = '0';
}
else if (type == 'PayPal' && notApp == '1') {
console.log("i am in the else if - functionality later");
}
else {
console.log("i am in the else - functionality later");
}
this.info.field = type;
this.info.number = notApp;
return this.info;
};
});
myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
$scope.ContinueAngularMethod = function () {
$rootScope.field = 'payment';
$scope.field = $rootScope.field;
$scope.notApp = '1';
console.log("I don't error in here");
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
$rootScope.$apply();
}
}]);
function MyCtrl($scope) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
$scope.myFunct = function (keyEvent) {
if (keyEvent.which === 13) {
//They hit the enter key so ignore this
keyEvent.preventDefault();
}
//Becauase a new child scope is generated you can't use $scope as that refers to the parent . But this refers to the child.
var rPromise = findAll(this.softwareText);
}
}
</script>
在无法工作的函数中调用服务:
myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
$scope.ContinueAngularMethod = function () {
$rootScope.field = 'payment';
$scope.field = $rootScope.field;
$scope.notApp = '1';
console.log("I don't error in here");
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
$rootScope.$apply();
}
}]);
function MyCtrl($scope) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
<script type='text/javascript'>
//Angular stuff
var myApp = angular.module('myApp', []);
//Set up my service
myApp.service('systemService', function () {
this.info = {}; //Declaring the object
this.checkDropDownValue = function (type, notAppJ) {
if (type != 'PayPal') {
console.log("I am not PayPal");
field = 'other'
notApp = '0';
}
else if (type == 'PayPal' && notApp == '1') {
console.log("i am in the else if - functionality later");
}
else {
console.log("i am in the else - functionality later");
}
this.info.field = type;
this.info.number = notApp;
return this.info;
};
});
myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
$scope.ContinueAngularMethod = function () {
$rootScope.field = 'payment';
$scope.field = $rootScope.field;
$scope.notApp = '1';
console.log("I don't error in here");
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
$rootScope.$apply();
}
}]);
function MyCtrl($scope) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
$scope.myFunct = function (keyEvent) {
if (keyEvent.which === 13) {
//They hit the enter key so ignore this
keyEvent.preventDefault();
}
//Becauase a new child scope is generated you can't use $scope as that refers to the parent . But this refers to the child.
var rPromise = findAll(this.softwareText);
}
}
</script>
这是我的完整代码:
myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
$scope.ContinueAngularMethod = function () {
$rootScope.field = 'payment';
$scope.field = $rootScope.field;
$scope.notApp = '1';
console.log("I don't error in here");
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
$rootScope.$apply();
}
}]);
function MyCtrl($scope) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
<script type='text/javascript'>
//Angular stuff
var myApp = angular.module('myApp', []);
//Set up my service
myApp.service('systemService', function () {
this.info = {}; //Declaring the object
this.checkDropDownValue = function (type, notAppJ) {
if (type != 'PayPal') {
console.log("I am not PayPal");
field = 'other'
notApp = '0';
}
else if (type == 'PayPal' && notApp == '1') {
console.log("i am in the else if - functionality later");
}
else {
console.log("i am in the else - functionality later");
}
this.info.field = type;
this.info.number = notApp;
return this.info;
};
});
myApp.controller('continueController', ["$scope", "$rootScope", 'systemService', function ($scope, $rootScope, systemService) {
$scope.ContinueAngularMethod = function () {
$rootScope.field = 'payment';
$scope.field = $rootScope.field;
$scope.notApp = '1';
console.log("I don't error in here");
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
$rootScope.$apply();
}
}]);
function MyCtrl($scope) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
$scope.myFunct = function (keyEvent) {
if (keyEvent.which === 13) {
//They hit the enter key so ignore this
keyEvent.preventDefault();
}
//Becauase a new child scope is generated you can't use $scope as that refers to the parent . But this refers to the child.
var rPromise = findAll(this.softwareText);
}
}
</script>
//角状物
var myApp=angular.module('myApp',[]);
//设置我的服务
myApp.service('systemService',函数(){
this.info={};//声明对象
this.checkDropDownValue=函数(类型,notAppJ){
如果(键入!=“贝宝”){
log(“我不是贝宝”);
字段='其他'
notApp='0';
}
else if(type='PayPal'&¬App='1'){
log(“我现在在elseif-稍后的功能中”);
}
否则{
log(“我现在在else中-稍后再使用功能”);
}
this.info.field=类型;
this.info.number=notApp;
返回此.info;
};
});
myApp.controller('continueController'、[“$scope”、“$rootScope”、'systemService',函数($scope、$rootScope、systemService){
$scope.ContinueAngularMethod=函数(){
$rootScope.field='payment';
$scope.field=$rootScope.field;
$scope.notApp='1';
log(“我这里没有错误”);
$scope.ss=systemService.checkDropDownValue($scope.payment.type,$scope.notApp);
$rootScope.$apply();
}
}]);
函数MyCtrl($scope){
$scope.changeme=函数(){
log(“下拉更改在此处执行某些操作…”);
//呼叫服务
$scope.ss=systemService.checkDropDownValue($scope.payment.type,$scope.notApp);
log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
$scope.myFunct=函数(keyEvent){
if(keyEvent.which==13){
//他们按了回车键,所以忽略这个
keyEvent.preventDefault();
}
//因为生成了一个新的子作用域,所以不能使用$scope,因为它引用的是父作用域。但它引用的是子作用域。
var rPromise=findAll(this.softwareText);
}
}
我尝试了这些,但没有任何运气:
您不推荐使用定义
MyCtrl
,您需要注入systemService依赖性才能使用它
尝试以定义continueController
的相同方式定义MyCtrl控制器,如下所示:
myApp.controller('MyCtrl', ["$scope", 'systemService', function ($scope, systemService) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
$scope.myFunct = function (keyEvent) {
if (keyEvent.which === 13) {
//They hit the enter key so ignore this
keyEvent.preventDefault();
}
//Becauase a new child scope is generated you can't use $scope as that refers to the parent . But this refers to the child.
var rPromise = findAll(this.softwareText);
}
}]);
您不推荐使用定义
MyCtrl
,您需要注入systemService依赖性才能使用它
尝试以定义continueController
的相同方式定义MyCtrl控制器,如下所示:
myApp.controller('MyCtrl', ["$scope", 'systemService', function ($scope, systemService) {
$scope.changeme = function () {
console.log("Drop down changes ideally do something here....");
//Call to service
$scope.ss = systemService.checkDropDownValue($scope.payment.type, $scope.notApp);
console.log($scope.ss.field);
console.log($scope.ss.notAppJ);
}
$scope.myFunct = function (keyEvent) {
if (keyEvent.which === 13) {
//They hit the enter key so ignore this
keyEvent.preventDefault();
}
//Becauase a new child scope is generated you can't use $scope as that refers to the parent . But this refers to the child.
var rPromise = findAll(this.softwareText);
}
}]);
MyCtrl应该提供对
systemService
的依赖关系
MyCtrl应该提供对
systemService
的依赖关系
什么是MyCtrl?这不是一个控制器,它只是一个随机函数。它是从哪里调用的?它在我的HTML中定义为
,不起作用;正如我已经提到的,MyCtrl
不是控制器。好的-我只能从控制器调用服务吗?不是功能?所以,为了解决这个问题,我应该把一个调用放到一个控制器中,然后控制器会调用我的服务吗?或者有更直接的方法吗?您必须使用Angular的旧版本。如果MyCtrl
以这种方式声明并且实际起作用,则Angular 1.3中删除了此声明样式。除此之外,您不能在该函数中使用systemService
,因为它没有声明,也没有注入。什么是MyCtrl
?这不是一个控制器,它只是一个随机函数。它是从哪里调用的?它在我的HTML中定义为
,不起作用;正如我已经提到的,MyCtrl
不是控制器。好的-我只能从控制器调用服务吗?不是功能?所以,为了解决这个问题,我应该把一个调用放到一个控制器中,然后控制器会调用我的服务吗?或者有更直接的方法吗?您必须使用Angular的旧版本。如果MyCtrl
以这种方式声明并且实际起作用,则Angular 1.3中删除了此声明样式。除此之外,您不能在该函数中使用systemService
,因为它没有声明,也没有注入。这就解决了它!非常感谢。时间到了我会记为正确答案的!这就解决了!非常感谢。时间到了我会记为正确答案的!