指令内的更改指令变量不起作用



我正在尝试更改指令内的控制器变量,这是我的代码:

主要控制器是:

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()方法。在这里我的使用方式:

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

我可以在指令控制器内访问$scope.filteredDevices,但是当我更改其值时,它不会在主控制器中更改。我在做什么错?

如果要在父控制器范围上保存更改,则应使用

范围:false,

将指令更改为:

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

这是一篇有用的文章。

最新更新