0

I have asked similar question before, this time I am stuck with recording data to the blockchain using Angular js and Angular Factory. Please see the code below and advise me where I am wrong

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
5
  • There is syntax errors in your code, can you edit it ? Commented Feb 23, 2018 at 12:10
  • Sure i will do it now Commented Feb 23, 2018 at 12:14
  • What are callback and errorCallback doing? Can you post their code aswell? Commented Feb 23, 2018 at 12:29
  • + There are lots and lots of syntax errors in your code .. Commented Feb 23, 2018 at 12:29
  • Please check my answer I modified your code. Commented Feb 23, 2018 at 12:33

2 Answers 2

1

There are so many errors in you're code, that I was considering not to 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

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for help I am testing this now
0

@Tabz: modified your code.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.