AngularJs。 ng包含在jQuery popover中

时间:2015-01-09 10:50:27

标签: javascript jquery angularjs

我开始学习AngularJs但面临一个问题。 ng Include指令在jQuery popover中不起作用。这是代码示例:(http://jsfiddle.net/kgupkx87/13/) 如您所见,$ scope.loaded函数已被执行,但HTML并未包含在popover模板中。

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
    <title>ng-include issue</title>
</head>
<body>
<div data-ng-app="myApp" data-ng-controller="defaultCtrl">
    <button data-my-directive data-popover-title="Settings" data-popover-template="settings.html" data-popover-placement="right">
        Settings
    </button>

    <script type="text/ng-template" id="settings.html">
        <p>{{mainFormTmpl}}</p>
        <ul>
            <li data-ng-repeat="num in [1,2,3]">{{num}}</li>
        </ul>
        <span ng-include="mainFormTmpl" onload="loaded()"></span>
    </script>

    <script type="text/ng-template" id="main-form.html">
        <p>List of directives</p>
    </script>
</div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>

<script type="text/javascript">
    var app = angular.module("myApp", []);
    app.controller("defaultCtrl", ["$scope", function($scope) {
        $scope.mainFormTmpl = "main-form.html";
        $scope.loaded = function() { console.log("Loaded"); }
    }]);
    app.directive("myDirective", ["$templateCache", "$compile", function($templateCache, $compile) {
        return {
            scope: true,
            restrict: "A",
            controller: function($scope) {
                $scope.save = function(e) {}
                $scope.cancel = function(e) {}
            },
            link: function(scope, el, attrs) {
                var tpl = $templateCache.get(attrs.popoverTemplate);
                el.popover({
                    trigger: 'click',
                    html: true,
                    title: attrs.popoverTitle,
                    content: $compile(tpl)(scope),
                    placement: attrs.popoverPlacement
                });
            }
        }
    }]);
</script>
</body>
</html>

我会很感激任何提示

1 个答案:

答案 0 :(得分:1)

您需要同样处理ng-include:

<p><ng-include src="mainFormTmpl" /></p>

代替

<p>{{mainFormTmpl}}</p>

Demo