0

Created an Angular Service:

calculator_app.service('FillOpportunity', function () {
    this.fill_opportunity = function (path,scope) {

        $.ajax({
            url: 'opportunitycalculator/calculator/GetProducts?idstring=' + path,
            type: "GET",
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data)
            {
                scope.opportunity_data = data;
                scope.$apply();

            },
            error: function () {
            }
        });
    };
});

Called the service on ng-change of a dropdown:

FillOpportunity.fill_opportunity($scope.result_path,$scope);

The scope.opportunity_data is binded to the select in UI:

<select id="seloppurtunityproducts" name="selproducttype" multiple="" style="height:300px" ng-model="opportunity_products" ng-options="a for a in opportunity_data"></select>

On ng-Change, Ajax is called when I check in Network of Chrome, but the value is not updated in the select box.

Any inputs?

1 Answer 1

2

Don't use jQuery's ajax. Use the built in $http. Using $http automatically begins the digest cycle of angular's builtin compiler. If you must use jquery... then you'd have to call $scope.$apply(); every time there is a data change.

Service:

calculator_app.factory("calcService", ["$http", function($http) {
    return {
        getItem: function(url, items) {
            return $http.get(url,
                // query string like { userId: user.id } -> ?userId=value
                { params: items });
        }
    }
}]);

Inject the service into your controller and use:

calculator_app.controller("MainCtrl", ["calcService", "$scope", function(calcService, $scope) {
    $scope.opportunity_data = [];

    var payload = {
        idstring: path
    };

    //call the service
    calcService.getItem('path/to/calc/api', payload).then(function(response) {
            $scope.opportunity_data = response.data;
        }).catch(function(response) {
            alert('error' + response.data);
        });
}]);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks KKKK, I will try that but my question is why not? When I do the same thing in local machine it is working, only when I deployed in the production, this is not working. Any pointers there?
@KrishnaPrasadAmbaripeta By using jQuery's ajax calls with angularjs, you are really making more work for yourself and opening up the possibility for data errors (like you are experiencing); the data is being modified outside of the digest cycle. Using the built-in $http library will help you avoid this altogether and bring your code in-line with standard angular practices. Hard to say why it works on one machine and not another, given the limited information here.

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.