0

I need help - how to call myFunction from AngularJS in area success?

var app = angular.module('app', []);
app.controller("MyController", function($scope, $http) {

  $scope.myFunction = function() {
    alert("HI");
  };

  $http.get('myWS.asmx/myService?callback=?', {
      params: {
        userName: 'test',
        procedureName: "EXECUTE myProcedure"
      }
    })
    .success(
      function(data) {
        //CALL MY FUNCTION HERE!
      })
    .error(
      function() {
        alert("ERROR");
      }
    );
});

I've tried several times but nothing; any solution or example?

George Netu
  • 2,758
  • 4
  • 28
  • 49
paris0000
  • 53
  • 11

3 Answers3

0
 $http.get('myWS.asmx/myService?callback=?', {
    params: {
      userName: 'test',
      procedureName: "EXECUTE myProcedure"
    }
  })
  .success(
    function(data) {
      $scope.myFunction();
    })
  .error(
    function() {
      alert("ERROR");
    }
  );

basically.

George Netu
  • 2,758
  • 4
  • 28
  • 49
Pierre Gayvallet
  • 2,933
  • 2
  • 23
  • 37
0

Here is example,

var app = angular.module('app', []);
            app.controller("MyController", function ($scope, $http) {




                $scope.myFunction = function () {
                    alert("HI");
                };



        $scope.myFunctionLoadData = function () {
                    $http.get('myWS.asmx/myService?callback=?', {
                    params: {
                        userName: 'test',
                        procedureName: "EXECUTE myProcedure"
                    }
                })
                .success(
                    function (data) {
                        $scope.myFunction();
                    })
                .error(
                    function () {
                        alert("ERROR");
                    }
                );
                };


                $scope.myFunctionLoadData();


            });
paris0000
  • 53
  • 11
  • As an aside, this will break if minified: use the controller syntax... app.controller("MyController", [ "$scope", "$http", function($scope, $http) { ... } ]); – Nathan Jan 18 '16 at 10:07
  • is the http request definitely succeeding? (i.e. you can put an alert in the success function and it fires) – Nathan Jan 18 '16 at 10:09
  • @Nathan Yes, I'm put alert! – paris0000 Jan 18 '16 at 11:04
  • I've created a fiddle https://jsfiddle.net/8frmtwLr/3/ and the code above works... have you checked your console for other errors? – Nathan Jan 18 '16 at 13:43
-1

You don't need $scope, just use this, and create an instance of this outside of the scope of your callback function:

var app = angular.module('app', []);
app.controller("MyController", function($http) {

  var scope = this;

  this.myFunction = function() {
    alert("HI");
  };

  $http.get('myWS.asmx/myService?callback=?', {
      params: {
        userName: 'test',
        procedureName: "EXECUTE myProcedure"
      }
    })
    .then(
      function(data) {
        scope.myFunction();
      },
      function() {
        alert("ERROR");
      }
    );
});

Success and error don't work in the newer Angular versions, you want .then, with two functions inside.

JMK
  • 27,273
  • 52
  • 163
  • 280
  • now work function, but not working receive data from http! – paris0000 Jan 18 '16 at 09:42
  • Are you sure data is coming back? – JMK Jan 18 '16 at 09:45
  • Success and error work in my above example, only I don't how call any function. What do you want to update? – paris0000 Jan 18 '16 at 09:53
  • -1: not using $scope is very un-Angular and could lead to problems. $scope is there for a reason, use it. The other solution which defines the function on the scope and invokes it accordingly is the correct solution. – Nathan Jan 18 '16 at 10:00
  • @Nathan John Papa suggests to use $scope as a service, but the controllerAs syntax when you don't need any of the functionality offered by $scope, as its one less dependency - http://stackoverflow.com/a/19940503/969613 – JMK Jan 18 '16 at 10:09
  • @JMK given that you don't know the requirements or experience of paris0000, guiding him away from anything non-standard isn't helpful: all the AngularJS examples out there use scope, the answer you linked will provide moderate benefits in exchange for (in this case, and in the case of most starters) additional confusion and/or potential for mistakes. Keep it simple. – Nathan Jan 18 '16 at 10:16
  • This is standard though, most modern examples of Angular will use controllerAs where as older examples tend to use $scope so I don't think it's wrong to introduce the syntax. Also using success and error, as in the other answer, is outdated and might not even work depending on the version of Angular used, .then is better. – JMK Jan 18 '16 at 10:18
  • @Nathan Relevant discussion on Reddit, the angular developers recommend avoiding $scope too - https://www.reddit.com/r/angularjs/comments/20ztwx/5_guidelines_for_avoiding_scope_soup_in_angular/ – JMK Jan 18 '16 at 10:35
  • Google's style guidelines also recommend avoiding $scope - https://google.github.io/styleguide/angularjs-google-style.html – JMK Jan 18 '16 at 10:37
  • @JMK I stand corrected, I've learned something new, thank you. If you make a small edit to your answer, I'll be able to retract my -1. Sorry. :-) – Nathan Jan 18 '16 at 10:52