2016-07-27 2 views
1

Примера: HTML:Как получить доступ к функции в контроллере из шаблона директивы

<div ng-repeat="notification in vm.notifications" class="notifications"> 
    <notification notification="notification"></notification> 
</div> 

уведомления Директивы заменяется другие директивы:

app.directive('notification', function($compile){ 
     return { 
      restrict: 'E', 
      transclude: true, 
      scope: { 
       notification: "=notification" 
      }, 
      link: function(scope, element) { 
       var temp = "<notifications" + scope.notification.type.normalCase() + ">"; 
       element.replaceWith($compile(temp)(scope)); 
      } 
     } 
    }); 

Например несколько директив:

app.directive('notificationsStudentRequest', function(){ 
     return { 
      template: '<div class="alert alert-info alert-dismissible"><button type="button" class="close" ng-click="vm.deleteNotification(notification)"><span aria-hidden="true">&times;</span></button>Запрос на участие в курсе: "{{notification.payload.course.name}}"</div>', 
      restrict: 'E', 
      replace: true, 
      transclude: true 
     } 
    }); 

    app.directive('notificationsStudentRequestAccepted', function(){ 
     return { 
      template: '<div class="alert alert-success alert-dismissible"><button type="button" class="close" ng-click="vm.deleteNotification(notification)"><span aria-hidden="true">&times;</span></button>[{{ notification.createdAt | date:"medium"}}] Запрос на участие в курсе: "{{notification.payload.course.name}}" был принят</div>', 
      restrict: 'E', 
      replace: true, 
      transclude: true 
     } 
    }); 

И в контроллере у меня есть fuction vm.de leteNotification (значение)

app.controller('NotificationsController', function(){ 
     var vm = this; 
     vm.notifications = { 
      //some object with notifications 
     } 
     vm.deleteNotification = function(notification){ 
      vm.notifications.splice(vm.notifications.indexOf(notification), 1); 
     } 

     return vm; 
    }); 

нг щелкните в шаблоне не видите эту фикцию. Эта проблема может исправить $parent.vm.del......., но мне нужны другие решения.

ответ

1

Переведите список уведомлений и функции управления в службу. Внесите эту службу в свои директивы и контроллер.

app.service('NotificationManager', function(){ 
    var self = this; 
    angular.extend(self, { 
     notifications: {}, 
     deleteNotification: function(notification){ 
      self.notifications.splice(self.notifications.indexOf(notification), 1); 
     }; 
    }); 
}); 

app.directive('notificationsStudentRequest', function(){ 
    return { 
     template: '<div class="alert alert-info alert-dismissible"><button type="button" class="close" ng-click="dvm.deleteNotification(notification)"><span aria-hidden="true">&times;</span></button>Запрос на участие в курсе: "{{notification.payload.course.name}}"</div>', 
     restrict: 'E', 
     replace: true, 
     transclude: true, 
     bindToController: true, 
     controllerAs: 'dvm' 
     controller: function(NotificationManager){ 
      this.deleteNotification = function(n){ NotificationManager.deleteNotification(n); } 
     } 
    } 
}); 

app.directive('notificationsStudentRequestAccepted', function(NotificationManager){ 
    return { 
     template: '<div class="alert alert-success alert-dismissible"><button type="button" class="close" ng-click="dvm.deleteNotification(notification)"><span aria-hidden="true">&times;</span></button>[{{ notification.createdAt | date:"medium"}}] Запрос на участие в курсе: "{{notification.payload.course.name}}" был принят</div>', 
     restrict: 'E', 
     replace: true, 
     transclude: true, 
     bindToController: true, 
     controllerAs: 'dvm' 
     controller: function(NotificationManager){ 
      this.deleteNotification = function(n){ NotificationManager.deleteNotification(n); } 
     } 
    } 
}); 

app.controller('NotificationsController', function(NotificationManager){ 
    var vm = this; 
    vm.notifications = NotificationManager.notifications; 

    return vm; 
}); 
1

Попробуйте вставить функцию deleteNotification внутри объекта - я объяснил немного больше here.

Возможно, что-то вроде этого, должно дать вам доступ к этой функции.

app.controller('NotificationsController', function(){ 
    var vm = this; 
    vm.someObject = {}; 
     //some code 
    vm.someObject.deleteNotification = function(notification){ 
     vm.notifications.splice(vm.notifications.indexOf(notification), 1); 
    } 

    return vm; 
});