2016-04-14 67 views
-1

我是Angular Js的新手,我剛剛完成了angularjs,控制器的基本標記,當我啓動指令部分時,我理解了概念,但無法獲取寫入模板的數據輸入的指令屬性..請引導我,以便我可以在AngularJS中再走一步。問題涉及角JS指令

在此先感謝!

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
</head> 
<body> 

<div ng-app="appname" directive-name></div> 
<script> 

var app = angular.module("appname",[]); 
app.directive("directiveName",function() 
{ 
    return 
    { 
    template : "Hi i am template" 
    }; 
}); 
</script> 

</body> 
</html> 

回答

0

存在諸多問題:

1.你應該使用 https爲CDN

2.你有 ng-app標籤

var app = angular.module("appname",[]); start =「3」>
  • 返回應該是

    return { //the brace should be after return template : "Hi i am template" };

  • ,因爲在現代瀏覽器分號不是強制性的(它們會自動添加),所以return意味着回報什麼。 最後:

     <script> 
    
        var app = angular.module("appname",[]); 
        app.directive("directiveName",function() 
        { 
         return{ 
         template : "Hi i am template" 
         }; 
        }); 
        </script> 
    
    +0

    我認爲你的觀點1和2是不正確的並且不相關。你能給一個解釋嗎? –

    +0

    當您使用使用https的plnkr或jsfiddle時,well 1是相關的。爲2,不知何故我得到'appname'沒有定義錯誤:s,但你是正確的 – Kossel

    +0

    該社區並不意味着你的問題在PLUNK或JSFIDLE。請不要提出這種自信的回答,因爲可能有人需要受益而且沒有被誤導。 –

    0

    沒有錯,你的邏輯或角,這是因爲JavaScript的自動分號插入。不以分號結尾但可能是語句結尾的行會自動終止。所以,你的return語句 -

    app.directive("directiveName",function() 
    { 
        return //automatic semicolon insertion here 
        { 
        template : "Hi i am template" 
        }; 
    }); 
    

    應該是真的 -

    app.directive("directiveName",function() 
    { 
        return{ 
        template : "Hi i am template" 
        }; 
    }); 
    
    0

    你接近:

    <!doctype html> 
    <html> 
    <head> 
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
    </head> 
    <body ng-app="appname" ng-controller="MyController"> 
    <h1>{{Header}}</h1> 
    <my-template></my-template> 
    <script> 
    
    var app = angular.module("appname",[]); 
    app.controller("MyController", function ($scope){ 
        $scope.Header = "My Directive"; 
    
    }); 
    app.directive("myTemplate",function() { 
        return { 
        template : "<span>Hi i am template</span>", 
        restrict: 'E', 
        }; 
    }); 
    </script> 
    
    </body> 
    </html> 
    

    我測試過上面的代碼,並會使您的指令。

    0

    一切都很好,除了退貨後的換行。

    return { 
        template : "Hi i am template" 
        };