[英]How to read object in angularjs controller?
I have no problems when I read an object in view. 当我读取视图中的对象时,我没有任何问题。 For example my code was: 例如我的代码是:
angular.module('myApp',[])
.controller('myCtrl',function($scope){
var AnswerScheme = $scope.AnswerScheme = tbl_qst_master_answer.getByAnswerId(AnswerIdSelectedByStudent);
});
In my view: 在我看来:
{{AnswerScheme[0]}} // Then the output will be: {"myTestData":123}
However, I got problems if I want to read in the controller 但是,如果我想读控制器,就会遇到问题
AnswerScheme[0] // Then no results
How to read the object in myCtrl
? 如何在myCtrl
读取对象?
In your controller, you should use $scope
to access the scope data, so, {{AnswerScheme[0]}}
in view is equal to $scope.AnswerScheme[0]
n controller 在控制器中,应该使用$scope
来访问范围数据,因此,视图中的{{AnswerScheme[0]}}
等于$scope.AnswerScheme[0]
n控制器
In your service you are returning an object instead of a promise. 在您的服务中,您将返回一个对象而不是一个promise。
If you return a promise, you can call a callback function.
Even though it gets displayed corectly in the view since the scope is always watched in the view. 尽管它始终在视图中核心显示,但因为始终在视图中观察范围。
As per our discussion if you add $timeout
in controller you can access that object 根据我们的讨论,如果您在控制器中添加$timeout
,则可以访问该对象
The timeout
loads the scope after sometime, and by that time the scope will get resolved. timeout
将在一段timeout
加载范围,到那时该范围将得到解决。
angular.module('myApp',[])
.controller('myCtrl',function($scope,$timeout){
var AnswerScheme = $scope.AnswerScheme = tbl_qst_master_answer.getByAnswerId(AnswerIdSelectedByStudent);
$timeout(function () {
console.log($scope.AnswerScheme)
},900);
});
You can read like this, this will contain the object. 您可以像这样阅读,其中将包含对象。 Also object does not have an index. 对象也没有索引。
you have to just use $scope.AnswerScheme
您只需要使用$scope.AnswerScheme
Console.log($scope.AnswerScheme);
if its an array you can just use $scope.AnswerScheme[0]
如果是数组,则可以使用$scope.AnswerScheme[0]
Console.log($scope.AnswerScheme[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.