AngularJS和角度工厂

时间:2018-02-23 12:09:17

标签: javascript angularjs

之前我曾问过类似的问题,这次我坚持使用Angular js和Angular Factory将数据记录到区块链中。请参阅下面的代码并告诉我错误的地方

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

2 个答案:

答案 0 :(得分:1)

你的代码中有很多错误,我考虑的不是它。

但是我觉得有必要帮助你,请以下面的代码作为指导。

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;
});

试用这些简单的教程,详细了解控制器服务 **和承诺

https://www.w3schools.com/angular/angular_controllers.asp https://www.w3schools.com/angular/angular_services.asp https://docs.angularjs.org/api/ng/service/ $ Q

** Confused about Service vs Factory

答案 1 :(得分:0)

@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
相关问题