0

我想在自定義事件上顯示引導彈出窗口(從服務器加載數據之後)。我不明白爲什麼這個代碼不起作用。 JSFiddle如何在自定義事件中顯示角度自舉彈出?

第二個問題,如果我設置replace = true如何觸發事件?由於IE8不支持自定義標籤並拋出異常。

<div ng-app="tcApp"> 
    <manual></manual> 
</div> 
var tcApp = angular.module('tcApp', [ 'ui.bootstrap' ]); 

tcApp.config(function ($tooltipProvider) { 
    $tooltipProvider.setTriggers({ 
     'openAfterLoad': 'closeOnClick' 
    }); 
}); 

tcApp.directive('manual', [ '$timeout', '$interval', function($timeout, $interval) { 
    return { 
     restrict: 'E', 
     scope: {}, 
     template: 
    '<a ' + 
' href="#" ' + 
' ng-if="isManualVisible" ' + 
' popover-append-to-body="true" ' + 
' popover-placement="bottom" ' + 
' popover-trigger="openAfterLoad" ' + 
' popover-title="{{title}}" ' + 
' popover="{{content}}" ' + 
' tooltip="{{title}}" ' + 
' >' + 
' <span class="{{textClass}}">' + 
'  <span ng-show="!loading" class="glyphicon glyphicon-question-sign"></span>' + 
'  <span ng-show="loading" class="glyphicon glyphicon-refresh"></span>' + 
' </span>' + 
'</a>', 
     replace: false, // true for IE8 only 
     link: function (scope, element, attr) { 

      scope.isManualVisible = true; 
      scope.title = 'First title value'; 
      scope.content = 'First content value'; 
      scope.textClass = 'text-info'; 

      scope.opened = false; 
      scope.loading = false; 
      scope.loaded = false; 
      scope.loadedTitle = false; 
      scope.loadedContent = false; 
      scope.checkLoadState = function() { 
       if (scope.loadedTitle && scope.loadedContent) { 
        scope.loaded = true; 
        if (scope.loading) 
         scope.loading = false; 
        element.triggerHandler('openAfterLoad'); 
       } 
      }; 
      scope.$watch('loadedTitle', scope.checkLoadState); 
      scope.$watch('loadedContent', scope.checkLoadState); 

      element.on('openAfterLoad', function(event) { 
       console.log('openAfterLoad'); 
       scope.opened = true; 
      }); 

      element.on('closeOnClick', function(event) { 
       console.log('closeOnClick'); 
       scope.opened = false; 
      }); 

      element.on('click', function(event) { 
       console.log('click'); 
       if (scope.loaded) { 
        if (scope.opened) 
         element.triggerHandler('closeOnClick'); 
        else 
         element.triggerHandler('openAfterLoad'); 
       } else { 
        scope.loadData(); 
       } 
      }); 

      scope.loadData = function() { 
       if (!scope.loaded && !scope.loading) { 
        scope.loading = true; 

        $interval(function() { 
         scope.title = 'New title value'; 
         scope.loadedTitle = true; 
        }, 5000, 1); 

        $interval(function() { 
         scope.content = 'New content value'; 
         scope.loadedContent = true; 
        }, 5000, 1); 
       } 
      }; 
     } 
    }; 
}]); 
+0

你的代碼是一個爛攤子,你是混合角和'replace' jQuery是過時了。並且您正在使用舊版本的'ui-bootstrap'。你有沒有嘗試過這些例子? – svarog

+0

我需要使用ie8的舊版本。這是庫的最後一個版本,它仍然支持ie8。並且必須使用'replace = true'(ie8不支持自定義標籤並拋出異常)。我怎樣才能綁定事件處理程序和觸發事件的角度和沒有jQuery(即沒有混合)? –

+0

使用內建的角度指令,'ng-click','ng-change'等。 – svarog

回答

0

好吧,我找到解決方案。

首先,在根模板元素上混合replace=trueng-if是一個壞主意。接下來,我分離了角度廣播/廣播事件和原生js事件。 popover仍然需要本地事件。

JSFiddle

<div ng-app="tcApp"> 
    <manual></manual> 
</div> 
var tcApp = angular.module('tcApp', [ 'ui.bootstrap' ]); 

tcApp.config(function ($tooltipProvider) { 
    $tooltipProvider.setTriggers({ 
     'openAfterLoad': 'closeOnClick' 
    }); 
}); 

tcApp.directive('manual', [ '$timeout', '$interval', function($timeout, $interval) { 
    return { 
     restrict: 'E', 
     scope: {}, 
     template: 
    '<a ' + 
' href="#" ' + 
' ng-show="isManualVisible" ' + 
' ng-click="clickHandler()" ' + 
' popover-append-to-body="true" ' + 
' popover-placement="bottom" ' + 
' popover-trigger="openAfterLoad" ' + 
' popover-title="{{title}}" ' + 
' popover="{{content}}" ' + 
' tooltip="{{title}}" ' + 
' >' + 
' <span class="{{textClass}}">' + 
'  <span ng-show="!loading" class="glyphicon glyphicon-question-sign"></span>' + 
'  <span ng-show="loading" class="glyphicon glyphicon-refresh"></span>' + 
' </span>' + 
'</a>', 
     replace: true, // true for IE8 only 
     link: function (scope, element, attr) { 

      scope.isManualVisible = true; 
      scope.title = 'First title value'; 
      scope.content = 'First content value'; 
      scope.textClass = 'text-info'; 

      scope.opened = false; 
      scope.loading = false; 
      scope.loaded = false; 
      scope.loadedTitle = false; 
      scope.loadedContent = false; 
      scope.checkLoadState = function() { 
       if (scope.loadedTitle && scope.loadedContent) { 
        scope.loaded = true; 
        if (scope.loading) 
         scope.loading = false; 
        scope.$broadcast('openAfterLoad'); 
       } 
      }; 
      scope.$watch('loadedTitle', scope.checkLoadState); 
      scope.$watch('loadedContent', scope.checkLoadState); 

      scope.$on('openAfterLoad', function(event) { 
       console.log('openAfterLoad'); 
       scope.opened = true; 
       $timeout(function() { 
        // trigger popover 
        element.triggerHandler('openAfterLoad'); 
       }); 
      }); 

      scope.$on('closeOnClick', function(event) { 
       console.log('closeOnClick'); 
       scope.opened = false; 
       $timeout(function() { 
        // trigger popover 
        element.triggerHandler('closeOnClick'); 
       }); 
      }); 

      scope.clickHandler = function(event) { 
       console.log('click'); 
       if (scope.loaded) { 
        if (scope.opened) 
         scope.$broadcast('closeOnClick'); 
        else 
         scope.$broadcast('openAfterLoad'); 
       } else { 
        scope.loadData(); 
       } 
      }; 

      scope.loadData = function() { 
       if (!scope.loaded && !scope.loading) { 
        scope.loading = true; 

        $interval(function() { 
         scope.title = 'New title value'; 
         scope.loadedTitle = true; 
        }, 2000, 1); 

        $interval(function() { 
         scope.content = 'New content value'; 
         scope.loadedContent = true; 
        }, 3000, 1); 
       } 
      }; 
     } 
    }; 
}]); 
相關問題