I used to have this inside my html 'calculations-td-plan.html'
<tr ng-repeat="foodCalculation in selectedMealCalc.calculated_foods">
<td>{{foodCalculation.food.name}}</td>
<td>{{foodCalculation.gram_amount}} g</td>
<td>{{foodCalculation.kcal}} kcal</td>
<td>{{foodCalculation.proteina}} g</td>
<td>{{foodCalculation.cho}} g</td>
<td>{{foodCalculation.lipideos}} g</td>
<td class="text-center">
<button class="btn btn--principal btn--xs" ng-click="edtiFoodCalc(selectedmealcalc, foodCalculation, $index)">Editar</button>
<button class="btn btn--principal btn--xs" ng-click="removeFoodCalc(selectedmealcalc, foodCalculation)">Remover</button>
</td>
</tr>
Then I create a directive like above.
<div class="row">
<div class="col-md-12" ng-show="( items | filter: { food_option: option } ).length > 0">
Opção {{ option }}
<table class="table table-calculo table-striped">
<thead>
<tr>
<th>Alimento</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="foodCalculation in ( items | filter: { food_option: option } ) track by $index">
<td>{{foodCalculation.food.name}}</td>
<td class="text-center">
<button class="btn btn--principal btn--xs" ng-click="edtiFoodCalc(selectedmealcalc, foodCalculation, $index)">Editar</button>
<button class="btn btn--principal btn--xs" ng-click="removeFoodCalc(selectedmealcalc, foodCalculation)">Remover</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
And I call this diretive inside 'calculations-td-plan.html' as
<div ng-repeat="option in [0,1,2,3,4]">
<meal-option option="{{option}}"
items="selectedMealCalc.calculated_foods"
selectedmealcalc="selectedMealCalc"></meal-option>
</div>
And this is my directive JS.
'use strict';
angular.module('nutriApp').directive('mealOption', ['$compile', function($compile) {
var mealOption = {
restrict: 'E',
templateUrl: 'views/checkins/meal-options.html',
require: 'foodSelector',
scope: {
option: "@",
items: "=",
selectedmealcalc: "="
}
};
mealOption.controller = ['$scope', 'Food', function($scope, Food) {
$scope.sumFood = {};
$scope.summerizeOption = function(foods) {
if(foods.length > 0){
$scope.sumFood = Food.summerize(foods);
}
};
}];
return mealOption;
}]);
But now the funcions that I am calling edtiFoodCalc
and removeFoodCalc
are not working.
When I put ng-controller
in my directive it works, (Only call the method) but I can't get the same scope.
<div class="row" ng-controller="CheckinsPlansCtrl">
I am trying to edit with edtiFoodCalc
and I am not getting the result that I want. I think ng-controller
creates new scopes when I do ng-repeat
, and when I did not have a directive the code worked.
<div ng-repeat="option in [0,1,2,3,4]">
<meal-option option="{{option}}"
items="selectedMealCalc.calculated_foods"
selectedmealcalc="selectedMealCalc"></meal-option>
</div>