[英]Unable to access scope in same controller : AngularJS
請任何人告訴我我想念的是什么,或者我要面對的實際問題是什么。
這是我的控制器。
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`.
}]);
我堅持這個問題。 所有數據都可以用HTML進行訪問,但不能在控制器和指令中獲取。 指令還提供未定義的數據。
while printing scope i can see the object. but when i am trying to access that object it showing it's undefined.
讓我們看看javascript的工作原理
首先,您創建了一個具有3個屬性的范圍
*請注意, $scope.programs
, $scope.clients
和$scope.months
*
$scope.limit = 3;
$scope.loadMore = function() {
$scope.limit += 2;
}
$scope.programIs = 1;
然后您在工廠中調用這些方法來獲取數據,而在獲取這些數據時,將執行以下代碼
console.log($scope);// u got an object
console.log($scope.lead); // u got an undefined, of course u never set it
幾秒鍾后,您的請求之一完成了,讓我們以Getlead
為例,現在執行以下代碼
$scope.lead = response.data.data;
這是你得到$scope.lead
的時間
但為什么
console.log($scope)
在獲取數據后顯示所有內容?
您是否注意到控制台中有一個“信息”圖標? 表示the value is evaluated now...
或類似the value is evaluated now...
。 這意味着當您第一次記錄$scope
,它沒有lead
屬性,但是當獲取過程完成並且您單擊並擴展日志數據時,chrome將根據其當前值reevaluate
該值,因此您會看到$scope.lead
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.