0

I am doing a dashboard with live update, so after $http call when a db returns null object i want to assign default value as zero

 $scope.SAP=0;
$http({
			method: 'GET',
            url: 'getTilesDataForPrjectReport.do',
		}).then(function(response){
				 if(response.data.proj==='SAP'){
					$scope.SAP=response.data;
				}
			 }
		});
<div style="height:20%; background-color:#ff9e97;">
	<p id="colorPalletDashboardTileRed"> {{SAP.red}}</p>
	<p id="colorPalletDashboardTileAmber">{{SAP.amber}}</p>
	<p id="colorPalletDashboardTileGreen">{{SAP.green}}</p>
	</div>
	<div style="height:18%; background-color:#ff8a81;">
	<p> Total Projects : {{SAP.totalProj}} </p>
	</div>

If SAP is not in response data, i need to display 0 in all SAP bindings, even if i assign 0 to $scope.SAP, it's not reflecting in html. I know it could be done with ng-hide with another <p> tag, but what's the simplest solution

1
  • could you paste your json response here? Commented Jan 21, 2016 at 7:00

3 Answers 3

1
<!-- language: lang-js -->

    <div style="height:20%; background-color:#ff9e97;">
        <p id="colorPalletDashboardTileRed"> {{SAP.red || 0}}</p>
        <p id="colorPalletDashboardTileAmber">{{SAP.amber || 0}}</p>
        <p id="colorPalletDashboardTileGreen">{{SAP.green || 0}}</p>
        </div>
        <div style="height:18%; background-color:#ff8a81;">
        <p> Total Projects : {{SAP.totalProj || 0}} </p>
        </div>
     (or)
 //define $scope.default = 0;
<div style="height:20%; background-color:#ff9e97;">
        <p id="colorPalletDashboardTileRed"> {{SAP.red || default}}</p>
        <p id="colorPalletDashboardTileAmber">{{SAP.amber || default}}</p>
        <p id="colorPalletDashboardTileGreen">{{SAP.green || default}}</p>
        </div>
        <div style="height:18%; background-color:#ff8a81;">
        <p> Total Projects : {{SAP.totalProj || default }} </p>
        </div>

<!-- end snippet -->

I think, you can try this
or

I think you can try this

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

Comments

1

Make sure your html should be under the context of controller, where you are fetching a data and assigning to $scope.SAP.

You could use number filter there which will provide you , separated formatting too.

{{SAP.red | number}}

Sample Demo

2 Comments

not working..i guess you need to use double pipe like below answer
@VigneshRajarajan || means OR, | means filter
0

Instead of assigning $scope.SAP = 0, you can assign

$scope.SAP = {"red": 0, "amber": 0, "green": 0, "totalProj": 0}

or any default value.

1 Comment

when the number of scope objects and it's properties increase in future, it will be hard to do it. anyway thanks

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.