指令内的更改指令变量不起作用
Change Directive variable inside a directive is not working
我正在尝试更改指令中的控制器变量,这是我的代码:
主控制器是:
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
,但是当我更改它的值时,它在主控制器中并没有改变。我做错了什么?
如果你想保存对父控制器范围的更改,你应该使用
scope:false,
将指令更改为:
return {
restrict: "E",
controller: 'myDirectiveController',
scope: false,
templateUrl: '/app/templates/advanceSearchDirective.html'
};
我正在尝试更改指令中的控制器变量,这是我的代码:
主控制器是:
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
,但是当我更改它的值时,它在主控制器中并没有改变。我做错了什么?
如果你想保存对父控制器范围的更改,你应该使用
scope:false,
将指令更改为:
return {
restrict: "E",
controller: 'myDirectiveController',
scope: false,
templateUrl: '/app/templates/advanceSearchDirective.html'
};