0

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?

10
  • $scope.AnswerScheme[0] Commented Oct 22, 2016 at 14:05
  • What is getByAnswerId method? If Its a http call you should resolve the promisse before get the value, thats why you have no results Commented Oct 22, 2016 at 14:09
  • So I need to make http calls? I just called the service Commented Oct 22, 2016 at 14:11
  • but your service is not injected in your controller? Commented Oct 22, 2016 at 14:11
  • Of course I've done that, that was just my sample code only Commented Oct 22, 2016 at 14:12

2 Answers 2

1

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

In your service you are returning an object instead of a 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

The timeout loads the scope after sometime, and by that time the scope will get resolved.

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);

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

5 Comments

what is different from the answer i posted?
Let me try first...I think I've done that before but still does not work
nothing great, I just explained that what will be in view and what will be in controller and just removed the console.log, nothing wrong in your answer. I just added it in the controller.
SyntaxError: Unexpected end of input at Object.parse (native)
the above comment tells that, you have some error in your service
0

You can read like this, this will contain the object. Also object does not have an index.

you have to just use $scope.AnswerScheme

 Console.log($scope.AnswerScheme);

if its an array you can just use $scope.AnswerScheme[0]

 Console.log($scope.AnswerScheme[0]);

3 Comments

I knew about console.log..but I got problems with the variable AnswerScheme -- it return no value
you have to use $scope
I do not know, its not me do that.

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.