0

Please anyone tell me what am i missing or what is the actual issue that i am facing this problem.

Here is my controller.

app.controller('UpdateLeadController', ['$scope', '$http', '$location', '$route', 'ProgramsFactory', 'CommonFactory', '$filter', 'ProcessFactory', 'Upload', '$routeParams', function($scope, $http, $location, $route, ProgramsFactory, CommonFactory, $filter, ProcessFactory, Upload, $routeParams) {
 $scope.limit = 3;
$scope.loadMore = function() {
    $scope.limit += 2;
}
$scope.programIs = 1;

ProgramsFactory.Getprogramslist(1).then(function(response) {
    $scope.programs = response.data.data;
});
CommonFactory.Getclients().then(function(response) {
    $scope.clients = response.data.data;
});
ProgramsFactory.Getmonths().then(function(response) {
    $scope.months = response.data.data;
});

ProcessFactory.Getlead($routeParams.id).then(function(response){
    $scope.lead = response.data.data; // here i've got the lead data from factory.
});
console.log($scope); // when i am printing this scope i am getting the object lead with all data.
console.log($scope.lead); // when i am trying to print this. it outputs as `Undefined`.
}]);

I stuck on this issue. all data can be accessible in HTML but not able to get them in the controller as well as in directive. a directive is also giving data as undefined.

while printing scope i can see the object. but when i am trying to access that object it showing it's undefined.

1 Answer 1

2

let's see how javascript works

first u created a scope with 3 property

* note that $scope.programs, $scope.clients, $scope.months are not defined *

$scope.limit = 3;
$scope.loadMore = function() {
    $scope.limit += 2;
}
$scope.programIs = 1;

then u call those methods in factory to fetch data, while fetching these data, the following code are executed

console.log($scope);// u got an object 
console.log($scope.lead); // u got an undefined, of course u never set it

after a few seconds, one of your request is finished let's take Getlead for example, and now the following code is executed

$scope.lead = response.data.data; 

this is the time when u get $scope.lead

but why

console.log($scope)

is showing everything after fetching data ?

Did u notice that there is an 'info' icon in console? which says the value is evaluated now... or something like that. That means when u first log $scope, it doesn't have a lead property, but when the fetching process is finished and u click and expand the log data, chrome will reevaluate the value according to its current value , so u see the $scope.lead property.

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

1 Comment

I haven't a problem regarding data source. I am getting all data with that factory call. but unable to access them.

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.