0

i'm trying to return ajax call value put in $scope in angular js but getting nothing idon't what happing here can you please check my code which i write below are the code

function GetDataarray() {
        var jqxhr = $.ajax({
            type: 'GET',
            url: '@Url.Content("~/Home/GetPesrons")',
            data: '{}',
            dataType: 'json',
            success: function (data, textStatus, XMLHttpRequest) {
                //alert(JSON.stringify(data));
                return JSON.stringify(data);
            }
        });
    }

 var app = angular.module('Napp', []);
app.controller('GetAlphabetical', function ($scope) {

    $scope.filt = 'All';
    $scope.setFilter = function (letter) {
        $scope.filt = letter;
    };

    $scope.names = ['All', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    $scope.startsWith = function (customer) {
        var lowerStr = (customer.Name + "").toLowerCase();
        var letter = $scope.filt;
        if (letter === 'All') return true;
        return lowerStr.indexOf(letter.toLowerCase()) === 0;
    }

    $scope.Customer = GetDataarray();

});

And Html is here :

<div data-ng-app="Napp" ng-controller="GetAlphabetical">
Search : 
<input type="text" ng-model="txtsearch" />
<table>
    <tr>
        <td ng-repeat="letter in names">&nbsp;<span ng-click="setFilter(letter)">{{letter}}</span>|</td>
    </tr>
</table>
<br />
<ul>
    <li ng-repeat="cust in Customer | filter:startsWith | orderBy: 'Name' | filter : txtsearch">{{cust.Name}}</li>
</ul>

4
  • What does /Home/GetPesrons return? Can you check ajax call? May be ist misspelled and should be GetPersons Commented Aug 14, 2015 at 11:05
  • 1
    Oops . Don't mix drink and drive . You are using jQuery's $.ajax . Don't mix jquery and angular . check for $http service in angular . You will get your answer . Binding won't work the way your are doing it . Commented Aug 14, 2015 at 11:07
  • Return /Home/GetPesrons is an array should be like Customer= [{ ID: 1, Name: 'Nayeem', City: 'Indore' }, { ID: 2, Name: 'Sanjay', City: 'Bhopal' }, { ID: 3, Name: 'Aditya', City: 'Jhansi' }]; Commented Aug 14, 2015 at 11:10
  • @NayeemMansoori Can you please let us know the Response of API Commented Aug 14, 2015 at 11:23

2 Answers 2

1

Controller Code

    var app = angular.module('Napp', []);
    app.controller('GetAlphabetical', function ($scope, $http) {

        $scope.filt = 'All';

        $scope.setFilter = function (letter) {
            $scope.filt = letter;
        };

        $scope.names = ['All', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

        $scope.startsWith = function (customer) {
            var lowerStr = (customer.Name + "").toLowerCase();
            var letter = $scope.filt;
            if (letter === 'All') return true;
            return lowerStr.indexOf(letter.toLowerCase()) === 0;
        }

        function getCutomers() {
            $http.get('@Url.Content("~/Home/GetPesrons")').then(function (response) {
                var _data = angular.fromJson(response);
                $scope.customers = _data.data; // please check the request response if list id in data object 
            }, function (error) {
                throw error;
            })
        }

        getCutomers();

    });

HTML CODE

<ul>
  <li ng-repeat="cust in customers | filter:startsWith | orderBy: 'Name' | filter : txtsearch">{{cust.Name}}</li>
</ul>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks friend i'm new in angularjs
@NayeemMansoori most welcome. Let me help you if have any other issue
1

Angular data binding will not work the way you are using it . Please don't mix jquery and angular. So take a look at $http service(xhr service of angular) .

below is sample get call . So either put it in your controller or in your factory service .

// Simple GET request example :
 $http.get('/someUrl').
  then(function(response) {
  // this callback will be called asynchronously
   // when the response is available
  }, function(response) {
   // called asynchronously if an error occurs
   // or server returns response with an error status.
  });

In your controller add this

 var app = angular.module('Napp', []);
app.controller('GetAlphabetical', function ($scope, $http) {

    $scope.filt = 'All';

    $scope.setFilter = function (letter) {
        $scope.filt = letter;
    };

    $scope.names = ['All', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

    $scope.startsWith = function (customer) {
        var lowerStr = (customer.Name + "").toLowerCase();
        var letter = $scope.filt;
        if (letter === 'All') return true;
        return lowerStr.indexOf(letter.toLowerCase()) === 0;
    }

    function getCutomers() {
        $http.get('Home/GetPesrons').then(function (response) {
            var _data = angular.fromJson(response);
            $scope.customers = response.data; // please check the request response if list id in data object 
        }, function (error) {
            throw error;
        })
    }

    getCutomers();

});

2 Comments

You say right please give me idea how call mvc controller call in angularjs
check answer again . I have copied Mohan code. You just need to call this $http service in your controller or service and then assign the output to $scope so binding can take place . give right ajax url and see if it works for you .

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.