I have a function in controller in which I am calling it from ng-class directive. My code is working but I am not getting right output. Something is wrong but I am not being able to capture it because I am new in AngularJS. So anyone please have a look at my code and tell me what mistake I made and if possible discuss with rectified code.
My code
<div ng-app="myApp">
<ul ng-controller="MyController">
<li ng-class="setColor(item.businessTime,item.name)" ng-repeat="item in products">{{item.name}} — {{item.price}} — {{clsName}} — {{diff}}</li>
</ul>
</div>
var myApp = angular.module('myApp', []);
myApp.controller('MyController', function MyController($scope) {
$scope.dbTime='02/09/2013 15:00:00';
$scope.diff='';
$scope.clsName='';
$scope.setColor = function(businessTime,name) {
//alert('businessTime '+businessTime);
//alert('dbtime '+$scope.dbTime);
//var diff =$scope.dbTime.diff(businessTime, 'minutes')
//alert(diff);
var _diff=0;
var ms = moment($scope.dbTime,"DD/MM/YYYY HH:mm:ss").diff(moment(businessTime,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
//alert(d.asMinutes());
_diff=Math.round(d.asMinutes());
if(_diff.between(-2000, -6000))
{
//alert(name+' clsRed '+_diff);
$scope.diff=Math.round(d.asMinutes());
$scope.clsName="clsRed";
return "clsRed";
}
else if(_diff.between(-6001, -8000))
{
//alert(name+' clsGreen '+diff);
$scope.diff=Math.round(d.asMinutes());
$scope.clsName="clsYello";
return "clsYello";
}
else if(_diff.between(1000, 2000))
{
//alert(name+' clsYello '+_diff);
$scope.diff=Math.round(d.asMinutes());
$scope.clsName="clsGreen";
return "clsGreen";
}
}
$scope.products = [
{
'name' : 'Xbox',
'clearance' : true,
'price' : 30.99,
'businessTime':'05/09/2013 15:00:00'
},
{
'name' : 'Xbox 360',
'clearance' : false,
'salesStatus' : 'old',
'price' : 99.99,
'businessTime':'04/09/2013 14:20:00'
},
{
'name' : 'Xbox One',
'salesStatus' : 'new',
'price' : 50,
'businessTime':'06/09/2013 18:12:10'
},
{
'name' : 'PS2',
'clearance' : true,
'price' : 79.99,
'businessTime':'07/09/2013 19:22:00'
},
{
'name' : 'PS3',
'salesStatus' : 'old',
'price' : 99.99,
'businessTime':'01/09/2013 09:00:00'
},
{
'name' : 'PS4',
'salesStatus' : 'new',
'price' : 20.99,
'businessTime':'10/09/2013 07:00:00'
}
]
})
Number.prototype.between = function(a, b) {
var min = Math.min.apply(Math, [a, b]),
max = Math.max.apply(Math, [a, b]);
return this > min && this < max;
};
css
.clsRed {
font-weight: bold;
color: red;
}
.clsYello {
font-weight: bold;
color: yellow;
}
.clsGreen {
font-weight: bold;
color: green;
}
within if else logic i am setting class name $scope.clsName="clsYello"; but in output i have notice some time wrong class name in showing in html.
here is screen shot
now see the first data where class name is showing clsGreen but in output clsRed is applied. right output would be clsRed and also clsRed should be applied. clsRed has applied but clsGreen is showing as class name in output which is not right.
so please guide me where i made the mistake. please guide me with rectified code. js fiddle https://jsfiddle.net/tridip/czjo9f1m/8/
thanks
