not able to access $scope object outside a http response callback function in angularjs

后端 未结 3 1970
野趣味
野趣味 2021-01-17 02:51

I have this controller for saving some details for an individual. I have a separate repository EntityRepository where a function to get the user gender from database is defi

3条回答
  •  佛祖请我去吃肉
    2021-01-17 03:08

    You are not able to access the value outside due to asynchronous property of requests. What that simply means is that this statement: console.log("outside : ", $scope.userGender);, is getting executed before the .then(function(){ ... }).

    Fixes:

    1. Wrap your outside code inside of $timeout

    .controller('individualDetailsCtrl', function($scope, $rootScope, $location, EntityRepository, $timeout) {

    and

    $timeout(function() {console.log("outside : ", $scope.userGender)});

    1. Use $scope.userGender's value only inside the .then(function() { }).

提交回复
热议问题