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");
}
});
}
$httpservice for http requests, BTW. docs.angularjs.org/api/ng/service/$http