在模板中使用js-call的指令 - 方法不会被调用

时间:2016-05-31 16:36:56

标签: angularjs-directive angular-ui-bootstrap

在我的角度应用程序中,我有一个警报服务,它处理警报列表。然后我有一个指令,它向页面呈现所有警报。我使用UI Bootstrap组件。

但是,警报的关闭按钮不会调用方法:

.directive('someAlert', ['alertService', function (alertService){
        var templateString = '<uib-alert ng-repeat="alert in vm.alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</uib-alert>';
    return {
        restrict: 'E',
        template: templateString,
        scope: true, 
        controller: function(){
            var vm = this;

            vm.alerts = alertService.get();

            vm.closeAlert = function (index) {
                console.log('closeAlert within directive controller called');
                alertService.closeAlertIdx(index);
            }
        },
        controllerAs: 'vm',
        replace: true
    }
}]);

2 个答案:

答案 0 :(得分:0)

templateString

中试试
close="vm.closeAlert($index)"

答案 1 :(得分:0)

我直接向alert-instance添加了一个close方法。然后alert.close()按预期工作。找到了很好的解决方案:alertservice和稍微modified

相关问题