指令内的Change Directive变量不起作用

时间:2017-09-05 11:17:53

标签: angularjs angularjs-directive angularjs-controller

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

主控制器是:

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();  
     }
});

通过this错误运行apply()方法是错误的。 在这里我是如何使用它的:

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

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

1 个答案:

答案 0 :(得分:1)

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

  

范围:假,

将指令更改为:

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

here is an useful article