访问链接函数中的AngularJS指令范围
我是安格拉斯的新手。我在写指令。目前,我的指令如下所示:访问链接函数中的AngularJS指令范围,angularjs,Angularjs,我是安格拉斯的新手。我在写指令。目前,我的指令如下所示: .directive('myDirective', function(component) { return { restrict:'E', replace:true, templateUrl: 'myTemplate.tpl.html', scope: { myAttribute: '=' }, link: function(scope, element, attrs) {
.directive('myDirective', function(component) {
return {
restrict:'E',
replace:true,
templateUrl: 'myTemplate.tpl.html',
scope: {
myAttribute: '='
},
link: function(scope, element, attrs) {
console.log(scope.myAttribute);
}
};
})
<myDirective myAttribute="true"></myDirective>
我希望在HTML中使用此指令,如下所示:
.directive('myDirective', function(component) {
return {
restrict:'E',
replace:true,
templateUrl: 'myTemplate.tpl.html',
scope: {
myAttribute: '='
},
link: function(scope, element, attrs) {
console.log(scope.myAttribute);
}
};
})
<myDirective myAttribute="true"></myDirective>
我的问题是,如何在指令的link函数中获取'myAttribute'的值?我需要得到这个值来做一些编程工作。但是,我当前的方法总是将“未定义”打印到JavaScript控制台
谢谢你的帮助 仔细阅读驼峰大小写语法。您必须这样填充属性:
<myDirective my-attribute="true"></myDirective>
棘手的…首先,在HTML中,您需要将指令定义中的camel大小写名称转换为pascal大小写,因此您的HTML应该是:
<my-directive my-attribute="true"></my-directive>
啊!每次翻译都让我着迷。非常感谢。