能否在uib-popover-template中使用指令?
Hey there! Great question—totally valid as someone new to AngularJS and UI Bootstrap, don’t stress about whether it’s "proper" or not. Let’s break this down:
Core Answer
Unfortunately, the uib-popover-template directive does not support inline directive-style templates like <my-template-style>...</my-template-style> directly. This directive is explicitly designed to accept a URL string pointing to an external HTML template file (or a template cached in Angular’s template system), not raw inline template markup or custom directive tags.
Alternative Solutions
If you want to use inline template content (instead of a separate .html file), here are two reliable approaches:
1. Use uib-popover-html (for static HTML content)
If your popover content is static and doesn’t need Angular directives/bindings to be compiled, you can use uib-popover-html with Angular’s $sce service to trust the HTML:
<!-- In your view --> <button uib-popover-html="trustedPopoverContent" popover-title="Static Popover" > Open Static Popover </button>
// In your controller app.controller('MyController', function($scope, $sce) { $scope.trustedPopoverContent = $sce.trustAsHtml(` <div class="my-template-style"> <p>This is static inline content</p> <span>Angular bindings like {{name}} won't work here</span> </div> `); });
Note: Angular directives or expressions inside this HTML won’t be compiled—this is only for static content.
2. Use Angular’s text/ng-template (for dynamic, compiled content)
This is the better approach if you need Angular directives, bindings, or dynamic logic in your popover. You define your inline template using a <script> tag with type text/ng-template, then reference its ID in uib-popover-template:
<!-- Define your inline template in the same view --> <script type="text/ng-template" id="myInlinePopover.html"> <div class="my-template-style"> <p>Hello, {{userName}}!</p> <button ng-click="greetUser()">Say Hi</button> </div> </script> <!-- Use the template with uib-popover-template --> <button uib-popover-template="'myInlinePopover.html'" popover-title="Dynamic Popover" > Open Dynamic Popover </button>
// In your controller app.controller('MyController', function($scope) { $scope.userName = 'Cloud_cal'; $scope.greetUser = function() { alert('Hi there!'); }; });
This works because Angular automatically caches templates defined with text/ng-template, and uib-popover-template can reference these cached templates by their ID (treated like a URL). All Angular logic inside will be properly compiled and functional.
Why This Isn’t in the Docs
This behavior relies more on Angular’s core template caching system than a UI Bootstrap-specific feature, which is why it might not be explicitly called out in the UI Bootstrap docs. It’s a standard Angular pattern for working with directives that expect template URLs.
内容的提问来源于stack exchange,提问作者Cloud_cal

