[英]AngularJS and angular factory
I have asked similar question before, this time I am stuck with recording data to the blockchain using Angular js and Angular Factory. 我之前也曾问过类似的问题,这次我一直坚持使用Angular js和Angular Factory将数据记录到区块链中。 Please see the code below and advise me where I am wrong 请查看下面的代码,并告诉我我哪里错了
app.js app.js
var app = angular.module('application', [])
app.controller('appController',function($scope, appFactory) {
$('success_create').hide()
$scope.recordData = function(){
appFactory.recordData($scope.data, function(data){
$scope.recordData = data
$("success_create").show()
})}}
app.factory('appFactory',function($http) {
var test = []
factory.recordData = function(data, errorCallback) {
test = data.field1+"-"+data.field2
$http.get('/record_data'+data).then(function(output) {
if (output) {
callback(output)
}).catch(function(error) {
errorCallback(error) })}
return factory
There are so many errors in you're code, that I was considering not to awnser. 您的代码中存在太多错误,以至于我一直在考虑不使用awnser。
But as I felt the need to help you, take the code below as a guide. 但是,由于我觉得有必要为您提供帮助,请参考以下代码。
var app = angular.module('application', [])
app.controller('appController', function($scope, appFactory) {
// Use angular.element instead of the jQuery `$` selector
angular.element('success_create').hide();
$scope.recordData = function()
{
// The factory returns a promise,
// so you can do the same just as you would with $http
appFactory.recordData($scope.data).then(function(response) {
$scope.recordData = response.data;
angular.element("success_create").show()
});
}
});
app.factory('appFactory',function($http) {
// You define the var as array, but you assign a string later on
// So instead of var test = [] use var test = "" or just var test;
var test = ""; // Or var test;
var factory = {
recordData: function (data, errorCallback)
{
test = data.field1 + "-" + data.field2;
var promise = $http.get('/record_data' + data).then(function(output) {
return output.data;
});
// $http returns a promise, return this to your controller
// You can use the data it returns just like you
// would with the $http method
return promise;
}
}
// In your original code, you return the factory. But you never
// Defined the factory.
return factory;
});
Try out these simply tutorials to learn more about controllers , services ** and promises 试用这些简单的教程,以了解有关控制器 , 服务 **和Promise的更多信息
https://www.w3schools.com/angular/angular_controllers.asp https://www.w3schools.com/angular/angular_services.asp https://docs.angularjs.org/api/ng/service/ $q https://www.w3schools.com/angular/angular_controllers.asp https://www.w3schools.com/angular/angular_services.asp https://docs.angularjs.org/api/ng/service/ $ q
@Tabz: modified your code. @Tabz:修改了您的代码。
app.controller(appController,function($scope, appFactory) {
$("success_create").hide();
$scope.recordData = function(){
appFactory.recordData($scope.data, function(data){
$scope.recordData = data
$("success_create").show();
})
}
})
app.factory("appFactory", function($http) {
factory.recordData = function(data, errorCallback)
$http.get('/record_data'+data).then(function(output) {
if (output)
callback(output)
}).catch(function(error) {
errorCallback(error)
})};
return factory
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.