1

I am trying to get the response from a url and populate in the view, but I am do not see the view getting updated. Help me to resolve this issue. Thanks

Source

<!DOCTYPE html>
<html ng-app="myModule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>
<body ng-init="priceList='promo03'">
<div ng-controller="PricingController" >
    <div ng-repeat="item in finalList track by $index">
    <ul>
        <li>Actual Price: {{item.actualPrice}}</li>
        <li>Button: {{item.button}}</li>
        <li>Content: {{item.content}}</li>
        <li>Description: {{item.description}}</li>
        <li>Discounted Price: {{item.discountedPrice}}</li>
    </ul>
    </div>
</div>
<script>
var myModule = angular.module('myModule',[]);
myModule.service('bannerService', ['$http', function ($http) {
    this.getBannerList = function (id, success) {
        invokeHttp('https://poctest1.firebaseio.com/Pricing/' + id + '.json', success);
    }
    var invokeHttp = function (url, callback) {
        var value = $http({ method: 'GET', url: url }).
            success(function (data) {
                callback(data);
            }).error(function () {
                callback(null);
            });
            return value;
    };
}]);

myModule.controller('PricingController',['$scope','bannerService', function($scope, bannerService){
    $scope.finalList = [];
    var finalArray = [];
    $scope.splitArray= function(){
        $scope.array = $scope.priceList.split(",");
    };
    $scope.callService = function(){
        for(i = 0; i < $scope.array.length; i++){
            //console.log(bannerSupportService.fetch($scope.array[i])); 
            bannerService.getBannerList($scope.array[i], function(banner){
            if(banner != null){
                finalArray.push(angular.toJson(banner));
            }   
            if(i == $scope.array.length){
                $scope.finalList = finalArray;
                console.log('Final'+$scope.finalList);
            }
        });
        }
    };

    $scope.splitArray();
    $scope.callService();

}]);
</script>
</body>
</html>

Console Output

Final{"actualPrice":10,"button":"Check Availability","content":"£5 for 6 months, £10 a month for further 6","description":"Fiber broadband","discountedPrice":5}

HTML Actual Output

Actual Price:
Button:
Content:
Description:
Discounted Price:

HTML Expected Output

Actual Price: 10
Button: Check Availability
Content: £5 for 6 months, £10 a month for further 6
Description: Fiber broadband
Discounted Price: 5
5
  • 1
    $scope.finalList = finalArray; after this use - $scope.$apply() Commented Jun 19, 2015 at 4:57
  • I tried this already. I am getting this error code: docs.angularjs.org/error/$rootScope/inprog?p0=$digest Commented Jun 19, 2015 at 5:00
  • $timeout(function(){ $scope.$apply() }); Commented Jun 19, 2015 at 5:06
  • @RahulGNair Not working Commented Jun 19, 2015 at 5:13
  • you console.log looks like $scope.finalList is an object not an array Commented Jun 19, 2015 at 5:24

1 Answer 1

2

Final{"actualPrice":10,"button":"Check Availability","content":"£5 for 6 months, £10 a month for further 6","description":"Fiber broadband","discountedPrice":5}

This output represents object but not array, suppose you wait for array. Therefore ng-repeat prints nothing. Should be something like:

[
  {
    "actualPrice": 10,
    "button": "Check Availability",
    "content": "£5 for 6 months, £10 a month for further 6",
    "description": "Fiber broadband",
    "discountedPrice": 5
  }
]

Your service works properly so fix controller part

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

1 Comment

Thanks.. It helped me to solve the issue. Changed this line finalArray.push(angular.toJson(banner)); to finalArray.push(banner);

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.