0

Hi I'm showing some sample data which I get from server using angularjs.

Here I've called GetNames() function in ng-init. Is it ok to call that function in ng-init. Because I need to show data when page loads.

Here is my code.

<div ng-app="myApp" ng-controller="personController"  >

    <ul>
  <li ng-repeat="x in names">
    {{ x}}
  </li>
</ul>
</div>


<script>
var app = angular.module("myApp", []);

app.controller('personController', function($scope,$http) {
    $scope.GetNames = function () {
      var httpResponse = $http({
            method: 'POST',
           async: true,
           cache: false,
            url: "http://localhost:55513/Home/GetNames",
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });
      httpResponse.success(function (data) {
            $scope.names = data;       
       } ); 
    };
    $scope.GetNames();
});
</script>

And it works for me.

And also if I use jquery/Ajax for fetching data, even I get the response with data. That data is not loading in the screen. (If I do a postback with a button it loads) What is the reason for this..

here is my GetNames() function with the jquery/Ajax code..

$scope.GetNames = function () {

         $.ajax({
                url: "@Url.Action("GetNames", "Home")",

                dataType: 'json',
                type: 'POST',
                success: function (response) {
                    $scope.names = response;

                },
                error: function () {
                    console.log("error");
                }
            });
}
1

2 Answers 2

2

It is because you are using a jquery ajax to update the scope variable, which does not trigger the digest cycle which is responsible to update the UI.

The correct solution to this problem is to use $http service for ajax requests...

Another solution (If you want to know how to make the jquery ajax work) is to use $scope.$apply()

success: function (response) {
    $scope.$apply(function () {
        $scope.names = response;
    });
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks it works. How about the first question I've asked about the place calling GetNames() function since I have to load data when page loading?
@tarzanbappa since you have a controller why don't you just call GetNames after $scope.GetNames=function(){} so there won't be any need to have ng-init
@tarzanbappa can you share the complete controller code
@tarzanbappa but where is the GetNames defined... is it in a different controlelr
Sry for that.Now you can see the complete controller. Previously I called GetNames() instead of $scope.GetNames(). Now its working. could you plz check whether are there any improvements needed?
1

jQuery ajax is out of capability of angular, you need to wrap like below, it will tell the $scope.user is going to update and update the view also

 success: function (response) {
    $scope.$apply(function(){
        $scope.user = data;
    });
 },

Angular ajax comes with the digest cycle triggering mechanism. then it will automatically change the view also, but jquery is not capable of it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.