0

I have a simple query which i am not able to understand why it doesnt work for me.

I have a ajax call and i get data from backend. Here it is.

$.ajax({
    type: "GET",
    url: serviceURL,
}).then(function (response) {
    $scope.$apply(function () {
        $scope.username = response.FirstName;
    });
    console.log($scope.username);
});

I am able to get the console output here. Means ajax works fine. Now i need to display it at the front end. Here is the code for that.

<li ng-model="username">[USERNAME should comeHere]</li>

I have used ng-model to bind the controller side data to front end. So the username should display in between the [] brackets i mentioned above. However, this is not happening.

Can someone tell me what is it tat i am doing wrong. ? I have also used $apply because i read somewhere that this will force it to make the updated changes.

2
  • 1
    don't use $.ajax , use $http . Also read docs for ng-model it is not for displaying text. Commented Feb 23, 2016 at 21:31
  • can you show the console result. Commented Feb 23, 2016 at 21:31

1 Answer 1

1

It should be:

<li>{{username}}</li>

This may help to shed light on this: What's the difference between ng-model and ng-bind

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

3 Comments

strangely, it shows a blank line. let me have a look again
If it's not working, there might be a problem in your app setup, in which case you would need to post the core components of your code.
oh yea, it worked. thanks for clearing my concept of ng-model and ng-bind.

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.