I have a unique custom directive that I implemented in AngularJS. The directive is called within an ng-repeat
loop, as shown below:
The array of objects, selectedMealCalc.calculated_foods, is aliased as 'items'
<!-- CUSTOM DIRECTIVE -->
<div ng-repeat="option in [0,1,2,3,4]">
<meal-option option="{{option}}"
items="selectedMealCalc.calculated_foods"
selectedmealcalc="selectedMealCalc"></meal-option> </div>
<!-- CUSTOM DIRECTIVE -->
The custom directive code created in AngularJS is structured as follows:
'use strict';
angular.module('nutriApp').directive('mealOption', ['$compile', function($compile) {
return {
restrict: 'E',
templateUrl: 'views/checkins/meal-options.html',
scope: {
option: "@",
items: "=",
selectedmealcalc: "="
},
controller: ['$scope', 'Food', function($scope, Food) {
$scope.sumFood = {};
$scope.summerizeOption = function(foods) {
if(foods.length > 0){
$scope.sumFood = Food.summerize(foods);
}
return $scope.sumFood;
};
}]
};
}]);
The HTML structure for the directive looks like this:
<div class="row" ng-init="filteredItems = ( items | filter: { food_option: option } )" ng-controller="CheckinsPlansCtrl">
<div class="col-md-12" ng-show="filteredItems.length > 0">
Option {{ option }}
<table class="table table-calculo table-striped">
<thead>
<tr>
<th>Food</th>
<th>Amount</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="foodCalculation in filteredItems track by $index">
<td>{{foodCalculation.food.name}}</td>
<td>{{foodCalculation.gram_amount}} g</td>
</tr>
</tbody>
</table>
</div>
</div>
However, when I update the
selectedMealCalc.calculated_foods
, the custom directive does not automatically refresh. To see the changes, I need to close and reopen the modal on my page.