[英]Angular Directive parameter Issue
I am creating a directive which will take the arguments passed to it from html and populate the fields in the template. 我正在创建一个指令,该指令将采用从html传递给它的参数并填充模板中的字段。 The code for directive is like below.
指令的代码如下。
(function(){
angular.module("MyApp",{})
.directive("myDirective",function(){
var MyLink = function(scope,element,attr){
scope.title = attr.title;
}
return{
restrict : 'EA',
transclude:true,
templateUrl:"directive.html",
link:MyLink
}
});
}())
The directive.html is like Directive.html就像
<div >
<span data-ng-bind="title"></span>
</div>
The main page is like 主页就像
<div ng-app="IndexApp" ng-controller="IndexController">
<my-directive title="hello"></my-directive>
this is a test
</div>
My issue is that why is hello not getting displayed ? 我的问题是,为什么无法显示您好?
Your module declaration was wrong, you were using {}
instead of []
. 您的模块声明有误,您使用的是
{}
而不是[]
。 If you also wanted to declare the directive in another module then you need to add the dependency to your indexApp (I've done so in this plunker). 如果您还想在另一个模块中声明该指令,则需要将依赖项添加到indexApp中(我已经在此插件中做了此操作)。
http://plnkr.co/edit/s02VmgFfWhstmVhVVrUa?p=preview http://plnkr.co/edit/s02VmgFfWhstmVhVVrUa?p=preview
index.js index.js
(function(){
angular.module("IndexApp",['myDirectives'])
.controller("IndexController",function($scope){
$scope.title = "this is to test";
});
}())
directive.js directive.js
(function(){
angular.module("myDirectives", [])
.directive("myDirective",function(){
var MyLink = function(scope,element,attr){
scope.title = attr.title;
}
return{
restrict : 'EA',
templateUrl:"directive.html",
link:MyLink
}
});
}())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.