我以前在我的html计划中有这个。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>
然后我创建一个如上所示的指令。
<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>
我把这个方向称为“计算td计划”。html“作为”
<div ng-repeat="option in [0,1,2,3,4]">
<meal-option option="{{option}}"
items="selectedMealCalc.calculated_foods"
selectedmealcalc="selectedMealCalc"></meal-option>
</div>
这是我的指令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;
}]);
但是现在我调用的函数
edtiFoodCalc
和
removeFoodCalc
不起作用。
当我把
ng-controller
在我的指令中,它可以工作(只调用该方法),但我无法获得相同的范围。
<div class="row" ng-controller="CheckinsPlansCtrl">
我正在尝试编辑
edtiFoodCalc
我没有得到我想要的结果。我想
ng控制器
创建新作用域
ng-repeat
,当我没有指令时,代码就工作了。
<div ng-repeat="option in [0,1,2,3,4]">
<meal-option option="{{option}}"
items="selectedMealCalc.calculated_foods"
selectedmealcalc="selectedMealCalc"></meal-option>
</div>