在 .success 和 .error 中使用范围变量
Use a scope variable inside .success and .error
我想在请求完成或失败时使用范围变量 $scope.field.name。我试图安慰它,但它说未定义。如何访问这些语法中的范围变量?
usiApp.controller('fieldController',['$scope','$window','$http','field','$timeout','Profile','Referrence',function ($scope,$window,$http,field,$timeout,Profile,Referrence)
{
$scope.DoSomething = function(){
$scope.field.class = 'fieldController'; //class of php method
$scope.field.method = 'DoSomething'; //name of php method
$scope.field.name = 'Ellsworth';
field.getdata($scope.field)
.success(function(data){
if (data == true) {
console.log($scope.field.name); //returns error this is the problem
} else {
console.log($scope.field.name); //returns error
}
})
.error(function(data, status) {
$scope.messages = data || "Request failed";
$scope.status = status;
console.log($scope.status);
console.log($scope.messages);
console.log($scope.field.name); //returns error
});
}
}
我的field.getdata服务功能:
usiServices.factory('field', function($http) {
return {
getdata : function(option) {
return $http({
method: 'POST',
url: 'php/field-route.php',
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
data: $.param(option)
});
},
}
}
您错过了 $scope.field = {};
。该行将创建一个空对象。创建对象后,就可以创建对象的属性($scope.field.class, $scope.field.method,$scope.field.name
)。由于未创建对象,您将收到未定义的错误。
在以下内容之前添加此 $scope.field = {};
行:
$scope.field.class = 'fieldController'; //class of php method
$scope.field.method = 'DoSomething'; //name of php method
$scope.field.name = 'Ellsworth';
因此,最终更新的行将如下所示:
$scope.field = {};
$scope.field.class = 'fieldController'; //class of php method
$scope.field.method = 'DoSomething'; //name of php method
$scope.field.name = 'Ellsworth';
我想在请求完成或失败时使用范围变量 $scope.field.name。我试图安慰它,但它说未定义。如何访问这些语法中的范围变量?
usiApp.controller('fieldController',['$scope','$window','$http','field','$timeout','Profile','Referrence',function ($scope,$window,$http,field,$timeout,Profile,Referrence)
{
$scope.DoSomething = function(){
$scope.field.class = 'fieldController'; //class of php method
$scope.field.method = 'DoSomething'; //name of php method
$scope.field.name = 'Ellsworth';
field.getdata($scope.field)
.success(function(data){
if (data == true) {
console.log($scope.field.name); //returns error this is the problem
} else {
console.log($scope.field.name); //returns error
}
})
.error(function(data, status) {
$scope.messages = data || "Request failed";
$scope.status = status;
console.log($scope.status);
console.log($scope.messages);
console.log($scope.field.name); //returns error
});
}
}
我的field.getdata服务功能:
usiServices.factory('field', function($http) {
return {
getdata : function(option) {
return $http({
method: 'POST',
url: 'php/field-route.php',
headers: { 'Content-Type' : 'application/x-www-form-urlencoded' },
data: $.param(option)
});
},
}
}
您错过了 $scope.field = {};
。该行将创建一个空对象。创建对象后,就可以创建对象的属性($scope.field.class, $scope.field.method,$scope.field.name
)。由于未创建对象,您将收到未定义的错误。
在以下内容之前添加此 $scope.field = {};
行:
$scope.field.class = 'fieldController'; //class of php method
$scope.field.method = 'DoSomething'; //name of php method
$scope.field.name = 'Ellsworth';
因此,最终更新的行将如下所示:
$scope.field = {};
$scope.field.class = 'fieldController'; //class of php method
$scope.field.method = 'DoSomething'; //name of php method
$scope.field.name = 'Ellsworth';