AngularJS Function call looses object scope

fresher1987

I have a URL with query params as below.

http://somehost/page.html#/?param1=value1&param2=value2`

Inside the JS file I have a controller which defines a function as per the below code and makes a http call and assignes the data to the $scope.

angular.module('ngApp').controller('Cntrl', function ($scope,$location,$http){
    var val1 = $location.search()['param1'];

    $http.get('/api/call').success(function(data) {
         $scope.empList = data; 
         console.log ($scope.empList) ; // return data 
    });

    console.log ($scope.empList) ; // return undefined when accessed with above url 
    $scope.fetch = function() {
     // some code  which uses $scope.empList here 
    }

    $scope.fetch(); 
}

I have just started to learn angularJS and no idea why $scope.empList is undefined outside the http block.

Dmitriy

Your HTTP request is asynchronous. So you need to do like this:

angular.module('ui.kpi.bugs').controller('BugKpiForm', function ($scope,$location,$http){
var val1 = $location.search()['param1'];

$scope.empList = [];

$http.get('/api/call').success(function(data) {
     $scope.empList = data; 
     console.log ($scope.empList) ; // return data 

     $scope.fetch(); // invoke only when empList data is available.
});

$scope.fetch = function() {
 // some code  which uses $scope.empList here 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Call a controller function from a directive without isolated scope in AngularJS

From Java

angularJS: How to call child scope function in parent scope

From Dev

How do you call an AngularJS $scope.Function from href?

From Dev

AngularJS add function to scope

From Dev

How do I call a function using $scope from HTML added by a directive in angularjs

From Dev

passing $scope variable to a function in AngularJS

From Dev

call function when leaving scope

From Dev

AngularJS Select Value Being Lost After Call to Restful Service and Scope Object Update

From Dev

$scope not working within function AngularJS

From Dev

AngularJS Access $scope in function

From Dev

Alias the AngularJS $scope object in a controller

From Dev

AngularJS call scope function on route change

From Dev

AngularJS call function of object in ng-repeat

From Dev

Inheritance of $scope object in angularJs

From Dev

Angularjs call $scope.function from plain JS function

From Dev

AngularJs directive, scope with value and function

From Dev

AngularJS remove scope variable in function

From Dev

How to call another scope function in AngularJS

From Dev

AngularJS scope defined function is null

From Dev

The scope of a tempory object in a function call

From Dev

Add JSON Object to angularjs $scope?

From Dev

passing $scope object to a function in AngularJS

From Dev

call function when leaving scope

From Dev

angularjs directive scope function not working

From Dev

Scope function only called once, then looses binding

From Dev

angularjs scope function of a repeated directive

From Dev

Accessing scope variable in function in AngularJS

From Dev

AngularJS $scope not accessible outside the function

From Dev

AngularJs - call $scope function on the onclick event from <img> tag

Related Related

  1. 1

    Call a controller function from a directive without isolated scope in AngularJS

  2. 2

    angularJS: How to call child scope function in parent scope

  3. 3

    How do you call an AngularJS $scope.Function from href?

  4. 4

    AngularJS add function to scope

  5. 5

    How do I call a function using $scope from HTML added by a directive in angularjs

  6. 6

    passing $scope variable to a function in AngularJS

  7. 7

    call function when leaving scope

  8. 8

    AngularJS Select Value Being Lost After Call to Restful Service and Scope Object Update

  9. 9

    $scope not working within function AngularJS

  10. 10

    AngularJS Access $scope in function

  11. 11

    Alias the AngularJS $scope object in a controller

  12. 12

    AngularJS call scope function on route change

  13. 13

    AngularJS call function of object in ng-repeat

  14. 14

    Inheritance of $scope object in angularJs

  15. 15

    Angularjs call $scope.function from plain JS function

  16. 16

    AngularJs directive, scope with value and function

  17. 17

    AngularJS remove scope variable in function

  18. 18

    How to call another scope function in AngularJS

  19. 19

    AngularJS scope defined function is null

  20. 20

    The scope of a tempory object in a function call

  21. 21

    Add JSON Object to angularjs $scope?

  22. 22

    passing $scope object to a function in AngularJS

  23. 23

    call function when leaving scope

  24. 24

    angularjs directive scope function not working

  25. 25

    Scope function only called once, then looses binding

  26. 26

    angularjs scope function of a repeated directive

  27. 27

    Accessing scope variable in function in AngularJS

  28. 28

    AngularJS $scope not accessible outside the function

  29. 29

    AngularJs - call $scope function on the onclick event from <img> tag

HotTag

Archive