簡體   English   中英

AngularJS和angular工廠

[英]AngularJS and angular factory

我之前也曾問過類似的問題,這次我一直堅持使用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

您的代碼中存在太多錯誤,以至於我一直在考慮不使用awnser。

但是,由於我覺得有必要為您提供幫助,請參考以下代碼。

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

試用這些簡單的教程,以了解有關控制器服務 **和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

** 對服務與工廠感到困惑

@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM