【问题标题】:AngularJS Custom directives Not ShowingAngularJS自定义指令不显示
【发布时间】:2016-01-20 01:30:21
【问题描述】:

学习 AngularJS,我似乎看不出我的自定义指令有什么问题。使用修改后的 w3 学校代码来展示我的问题的更简单示例。

当我在谷歌浏览器中使用 demo.html 午餐时,我只得到一个白屏。

顶部截图 - demo.html 底部截图 - app.js

(function(){

    var app = angular.module("myApp", []);

    var direc = function() 
    {
        return 
        {
            restrict : 'A',
            template : "<h1>Made by a directive!</h1>"
        };
    };
    
    app.directive("w3TestDirective", direc);
})();
<!DOCTYPE html>
<html>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="app.js" type="text/javascript"></script>
    <body ng-app="myApp">
        
        <div w3-test-directive></div>

    </body>
</html>

【问题讨论】:

    标签: javascript html angularjs angularjs-directive


    【解决方案1】:

    语法错误:定义 direc 函数时不需要右括号。

    (function(){
    
        var app = angular.module("myApp", []);
    
        var direc = function() 
        {
            return 
            {
                restrict : 'A',
                template : "<h1>Made by a directive!</h1>"
            }
        };// You had extra closing parantheses here
    
          app.directive("w3TestDirective", direc);
    })();
    

    【讨论】:

    • 我让你改变了,但我仍然没有得到任何东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2019-10-11
    • 2015-11-02
    相关资源
    最近更新 更多