[英]Angularjs: use “.then” and get response
I want send a http request and get response of this request. 我想发送一个http请求并获得对此请求的响应。 I want if data is save, I get database information and if doesn't save, I get errors. 我想保存数据,获取数据库信息,如果不保存,则获取错误。 For this, I want use .then
in angularjs. 对于这一点,我想用.then
在angularjs。 I create angularjs controller and services to do this. 我创建angularjs控制器和服务来做到这一点。 I have below code in angularjs part: 我在angularjs部分中有以下代码:
bolouk.js
: bolouk.js
:
'use strict';
var app = angular.module('app');
app.controller('BoloukCtrl', ['$scope', 'Bolouks', function($scope, Bolouks){
$scope.save = function(){
Bolouks.create($scope.bolouk).then(function(data){
$scope.saveBolouk = data;
},function(err){
$scope.err = err;
}
);
};
}]);
boloukService.js
: boloukService.js
:
'use strict';
var app = angular.module('boloukService', ['ngResource']);
app.factory('Bolouks', function($resource) {
return $resource('/api/bolouks.json', {}, {
create: { method: 'POST', isArray: false }
});
});
and I have below code in rails server: bolouks_controller.rb
: 我在rails服务器中有以下代码: bolouks_controller.rb
:
def create
@bolouk = Bolouk.create(bolouk_params)
if @bolouk.valid?
#@bolouk.save
respond_with @bolouk, :location => api_bolouks_path
else
respond_with @bolouk.errors, :location => api_bolouks_path
end
end
private
def bolouk_params
params.require(:bolouk).permit(:boloukcode, :north, :south, :east, :west)
end
Request send to rails server correctly and data is save to database right, but I cannot get response of request and when I run function`, I get below error in chrome console: 请求正确发送到Rails服务器并且数据正确保存到数据库,但是我无法获得请求的响应,并且当我运行function`时,在chrome控制台中出现以下错误:
TypeError: undefined is not a function
at Scope.$scope.save (http://localhost:3000/assets/controllers/bolouk.js?body=1:19:39)
at http://localhost:3000/assets/angular.js?body=1:10973:21
at http://localhost:3000/assets/angular.js?body=1:20088:17
at Scope.$eval (http://localhost:3000/assets/angular.js?body=1:12752:28)
at Scope.$apply (http://localhost:3000/assets/angular.js?body=1:12850:23)
at HTMLFormElement.<anonymous> (http://localhost:3000/assets/angular.js?body=1:20087:21)
at HTMLFormElement.jQuery.event.dispatch (http://localhost:3000/assets/templates/jquery-1.10.2.js?body=1:4627:9)
at HTMLFormElement.elemData.handle (http://localhost:3000/assets/templates/jquery-1.10.2.js?body=1:4295:46)
I think I don't use .then
correctly in angularjs controller, I check $q
method too, but again get this error. 我觉得我不使用.then
在angularjs控制器正确的,我检查$q
方法太多,但再次出现此错误。 Any one have idea to solve this problem? 有人有解决这个问题的想法吗?
var app = angular.module('app',['boloukService']);
app.controller('BoloukCtrl', ['$scope', 'Bolouks', function($scope, Bolouks){
$scope.save = function(){
var user = Bolouks.get();
user.get(function(resp) {
//request successful
}, function(err) {
//request error
});
};
}]);
angular.module('boloukService', ['ngResource']);
app.factory('Bolouks', function($resource) {
var savedData = {}
function get() {
return $resource('/api/bolouks.json', {}, {
create: { method: 'POST', isArray: false}
});
}
return {
get: get
}
});
try this one :) 试试这个:)
you have assign both module in same variable. 您已经在相同的变量中分配了两个模块。 this may conflict your code. 这可能会与您的代码冲突。 so use this code: 因此,使用以下代码:
bolouk.js: bolouk.js:
'use strict';
var app = angular.module('app',['ngResource']);
app.controller('BoloukCtrl', ['$scope', 'Bolouks', function($scope, Bolouks){
$scope.save = function(){
Bolouks.create($scope.bolouk).$promise.then(function(data){
$scope.saveBolouk = data;
})
.catch(function (err) {
console.log(err);
})
};
}]);
boloukService.js: boloukService.js:
'use strict';
app.factory('Bolouks', function($resource) {
return $resource('/api/bolouks.json', {}, {
create: { method: 'POST', isArray: false }
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.