mahboub_mo
mahboub_mo

Reputation: 3038

Change Directive variable inside a directive is not working

I am trying to change a controller variable inside a directive and this is my code:

the main controller is :

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

and the directive is :

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

the thing is it faild to run the apply() method through this error. and here how i am using it :

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

I have access to $scope.filteredDevices inside the directive controller but when i change its value it doesn't change in the main controller. what am I doing wrong?

Upvotes: 2

Views: 126

Answers (1)

mhk
mhk

Reputation: 112

if you want to save the changes on the parent controller scope you should use

scope:false,

change the directive to :

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

here is an useful article .

Upvotes: 1

Related Questions