代码之家  ›  专栏  ›  技术社区  ›  mahboub_mo

指令内的Change指令变量无效

  •  2
  • mahboub_mo  · 技术社区  · 7 年前

    主控制器为:

    angular.module("app").controller('vehicleManagementController', ['$scope', 'toastr', '$filter' ,
    function ($scope, toastr, $filter) {
        .....
        $scope.filteredDevices = //Some List
        $scope.allDevices = [];
     }
    }]);
    

    angular.module('app').directive('advanceSearchDirective', ['deviceAdvancedSearchService', 'mapService', function (deviceAdvancedSearchService, mapService) {
    return {
        restrict: "E",
        controller: 'myDirectiveController',
        scope: { filteredDevices: '=filteredDevices' },
        templateUrl: '/app/templates/advanceSearchDirective.html'
    };
     }]);
    
    angular.module("app").controller(myDirectiveController( $scope) {
        $scope.search = function() {
           $scope.filteredDevices = [];
          $scope.$apply();  
         }
    });
    

    问题是它无法运行apply()方法 this 错误 下面是我如何使用它:

     <advance-search-directive filtered-devices="filteredDevices" model="$parent"></advance-search-directive>
    

    $scope.filteredDevices

    1 回复  |  直到 7 年前
        1
  •  1
  •   mhk    7 年前

    如果要保存对父控制器作用域的更改,应使用

    将指令更改为:

         return {
           restrict: "E",
           controller: 'myDirectiveController',
           scope: false,
           templateUrl: '/app/templates/advanceSearchDirective.html'
       };
    

    here is an useful article .