I am trying to filter my ng-repeat
based on a date selected in a datepicker. My HTML/Javascript:
<script>
$('#myFirstDate').datepicker({
startDate: '+1d',
todayHighlight: true
}).on('changeDate', function(e){
var theDate= $('#myFirstDate').datepicker('getDate').toString();
var regex = /[A-Z]\w+/;
theDate = regex.exec(theDate);
});
</script>
<div class="itemcontainer" ng-repeat="BreadToOrder in BreadsToOrder | filter:{ days: theDate}">
<img ng-src="img/{{BreadToOrder.text|lowercase|nospace}}.jpg"></img><span class="itemname">{{BreadToOrder.text}}</span><span class="itemprice">{{BreadToOrder.price}}</span><input type="number" placeholder="#">
</div>
My controller looks like this:
$scope.BreadsToOrder = [
{
text: 'Apple Bread',
price: '$7.25',
days: ["Mon","Tue","Wed","Thu","Fri","Sat","Sunday"]
},
{
text: 'Challah',
price: '$5.95',
days: ["Thu","Fri"]
}
]
So basically I would like a bread to be displayed only if the right day of the week is selected. This works great if I put in the actual date instead of theDate
such as filter:{ days: 'Mon'}
but I suppose Angular can't see the theDate
variable? Is it a scope issue? Should I put that jquery in the controller or what is the best way to do this, since I mostly still think like a JQuery rather than an Angular person?
It works with these additions to the HTML/Javascript and Controller:
HTML/Javascript:
Controller:
So basically I had to add the
angular.element
and use$apply
to get it to update the controller value. :)