2

抱歉,如果我在这里遗漏了一些愚蠢的东西,但我真的尝试了各种组合来使这段代码工作,但没有运气。

我正在学习但停留在这段代码directiveAngularjS-Recipes with AngularJS

https://github.com/fdietz/recipes-with-angular-js-examples/tree/master/chapter3/recipe4

我相信它应该在Hello World文本之前打印Heading 。但它不会来。让我知道我的代码中缺少什么- p

PLNKR 代码

代码作为一个整体 -

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="utf-8" />
    <title>Directive Example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
    <script>
        var myApp = angular.module("myApp", []);

        myApp.directive("myWidget", function(){
            return {
                restrict: "E",
                transclude: true,
                template: "<div ng-transclude><h3>Heading</h3></div>"
            };
        });
    </script>
</head>
<body>
    <my-widget>
        <p>Hello World!!</p>
    </my-widget>
</body>
</html>
4

2 回答 2

6

检查“div”之前的第一个“h3”

template: "<h3>Heading</h3><div ng-transclude></div>"
于 2014-09-08T13:53:10.017 回答
2

您需要更改配方的原因是 Angular 在 v1.0 和 v1.2 之间更改了嵌入的工作方式。

通过更改eed299a3,Angular 现在“在嵌入之前清除了嵌入点”。

如果您加载 v1.0(这是 github 存储库使用的),您将看到“标题”。使用 v1.2,您不会看到“标题”,除非您按照@Noypi 解释的方式修改模板。

于 2015-03-31T02:36:09.453 回答