Angular resource .get does not change the passed value of user ID after ng-init

MikeDiam

I use ng-init in the template to pass the curUID to the controller. Afterwards, controller should get information in the service, using curUID as an URL parameter.

Controller:

        $scope.getCurUID = function() {
        return parseInt($scope.curUID, 10);
    }
    $scope.curUser = hosterFactory.getHoster().get({id:$scope.getCurUID(), isArray:true}).$promise.then(
            function(response){
                respose = $scope.changeData(response);
                $scope.curUser = response; 
                console.log("curUser: (curUID): "+parseInt($scope.curUID, 10));
                console.log(response);
            },
            function(response) {
                console.log("error curUser: ");
                console.log(response);
                $scope.message = "Error: "+response.status + " " + response.statusText;
            }
    );

Service:

    .service('hosterFactory', ['$resource', 'baseURL', function($resource,baseURL) {

        this.getHoster = function(){
            var res = $resource("/api/rest/user/:id", null,  {'get':{method:'GET', isArray:true},'update':{method:'PUT' }});
            return res;
        };

    }])

If I directly write in .get id as 207 (for example) all works fine. But if I use the approach above - .get use curUID as 0 - it is wrong.

JB Nizet

Here's what happens, in chronological order:

  • the controller is instanciated
  • the controller function is thus executed and calls hosterFactory.getHoster().get({id:$scope.getCurUID() ...
  • ng-init sets the current user ID

You shouldn't use ng-init. It's explicitly documented as something you should (almost) never use. There is a better way. It's hard to tell what, since I don't know what the controller is used for, and what your application looks like.

If you want to do still use ng-init, then it should look like this:

ng-init="init(theUserId)"

and the $scope.init() function should take the passed userId, and make the HTTP request.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get html element by Id passed from ng-init of angular

From Dev

Ng-bind does not update value after change

From Dev

Change ng-view after get data in Angular.JS

From Dev

Effective user id does not change after making the file owned by root

From Dev

Effective user id does not change after making the file owned by root

From Dev

Angular : ng-init does not run on load

From Dev

Why passed value from ng-controller is blank in Angular JS

From Dev

angular change property of scope by reference from ng-init

From Dev

angular change property of scope by reference from ng-init

From Dev

change ng-init value when previous url is different

From Dev

Angular ng-init function loaded after directive

From Dev

Angular ng-if not updating on value change

From Dev

Angular - ng-init statement - the statement does not work

From Dev

Arguments passed to a function change in value after the function returns to the calling sub

From Dev

Does ng-init watch over change on instantiated property like ng-model does?

From Dev

Angular - ng-init checkbox value from controller object

From Dev

setting Angular select ng-init to first value in array

From Dev

Using angular, How do I get selected value on ng-change on a dropdown?

From Dev

Can't seem to get value from angular binding control in ng-change event

From Dev

Why does the method change passed array's value

From Dev

Simple angular example not working: ng-repeat + $resource + GET

From Dev

Angular ng-show (or ng-if) not updating on value change

From Dev

Passed value still edited after cancel angular mddialog

From Dev

C pointer does not get set after being passed to a function

From Dev

How change a path value after init Google Maps

From Dev

Angular JS: get ng-model on ng-change

From Dev

get selected value in angular ng-option

From Dev

Get value outside ng-repeat angular

From Dev

get selected value in angular ng-option

Related Related

  1. 1

    How to get html element by Id passed from ng-init of angular

  2. 2

    Ng-bind does not update value after change

  3. 3

    Change ng-view after get data in Angular.JS

  4. 4

    Effective user id does not change after making the file owned by root

  5. 5

    Effective user id does not change after making the file owned by root

  6. 6

    Angular : ng-init does not run on load

  7. 7

    Why passed value from ng-controller is blank in Angular JS

  8. 8

    angular change property of scope by reference from ng-init

  9. 9

    angular change property of scope by reference from ng-init

  10. 10

    change ng-init value when previous url is different

  11. 11

    Angular ng-init function loaded after directive

  12. 12

    Angular ng-if not updating on value change

  13. 13

    Angular - ng-init statement - the statement does not work

  14. 14

    Arguments passed to a function change in value after the function returns to the calling sub

  15. 15

    Does ng-init watch over change on instantiated property like ng-model does?

  16. 16

    Angular - ng-init checkbox value from controller object

  17. 17

    setting Angular select ng-init to first value in array

  18. 18

    Using angular, How do I get selected value on ng-change on a dropdown?

  19. 19

    Can't seem to get value from angular binding control in ng-change event

  20. 20

    Why does the method change passed array's value

  21. 21

    Simple angular example not working: ng-repeat + $resource + GET

  22. 22

    Angular ng-show (or ng-if) not updating on value change

  23. 23

    Passed value still edited after cancel angular mddialog

  24. 24

    C pointer does not get set after being passed to a function

  25. 25

    How change a path value after init Google Maps

  26. 26

    Angular JS: get ng-model on ng-change

  27. 27

    get selected value in angular ng-option

  28. 28

    Get value outside ng-repeat angular

  29. 29

    get selected value in angular ng-option

HotTag

Archive