代码之家  ›  专栏  ›  技术社区  ›  Danilo Cândido Rewar

无法在自定义指令内调用函数。范围不是我想要使用的

  •  0
  • Danilo Cândido Rewar  · 技术社区  · 7 年前

    我以前在我的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>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   james00794    7 年前

    你可以用angular &

    例如,给定控制器和指令:

    // Controller
    app.controller('MainCtrl', function($scope) {  
      $scope.funcA = function(value, index) {
        console.log("Called funcA from " + value + " with index " + String(index));
      };
    
      $scope.funcB = function(value, index) {
        console.log("Called funcB from " + value + " with index " + String(index));
      }
    });
    
    // Directive
    app.directive('arrayDirective', function() {
      return {
        templateUrl: "array_directive.html",
        scope: {
          controllerFuncA: "&controllerFuncA",
          controllerFuncB: "&controllerFuncB"
        },
        link: function (scope, element, attr) {
          scope.items = ["a","b","c","d","e"];
    
          scope.callControllerFuncA = function(i) {
            console.log("Calling controller funcA!");
            scope.controllerFuncA({from: "directive", index: i});
          };
    
          scope.callControllerFuncB = function(i) {
            console.log("Calling controller funcB!");
            scope.controllerFuncB({from: "directive", index: i});
          };   
        }
      };
    });
    

    以及以下模板:

    <!-- Controller template -->
    <body ng-controller="MainCtrl">
      <array-directive controller-func-a="funcA(from, index)" controller-func-b="funcB(from, index)"></array-directive>
    </body>
    
    <!-- Directive template -->
    <div>
      <div ng-repeat="item in items">
        <div>{{item}}</div> 
        <div><a href="#" ng-click="callControllerFuncA($index)">Call Controller Func A</a></div>
        <div><a href="#" ng-click="callControllerFuncB($index)">Call Controller Func B</a></div>
      </div>
    </div>
    

    然后您可以使用 controllerFuncA controllerFuncB 从指令中调用相应的父控制器函数, funcA funcB . 注意,这些指令函数采用 对象 作为参数,其中对象的键对应于在 <array-directive> 标签

    请参阅随附的fiddle并查看开发人员控制台以获取完整示例: https://plnkr.co/edit/3h8J00ndBk4OQ16C8hf2