问题涉及角JS指令

问题涉及角JS指令

问题描述:

我是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> 

存在诸多问题:

1.你应该使用 https为CDN

2.你有 ng-app标签

    var app = angular.module("appname",[]); start =“3”>
  1. 返回应该是

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

  2. ,因为在现代浏览器分号不是强制性的(它们会自动添加),所以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。请不要提出这种自信的回答,因为可能有人需要受益而且没有被误导。 –

没有错,你的逻辑或角,这是因为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" 
    }; 
}); 

你接近:

<!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> 

我测试过上面的代码,并会使您的指令。

一切都很好,除了退货后的换行。

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