Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问链接函数中的AngularJS指令范围_Angularjs - Fatal编程技术网

访问链接函数中的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>

啊!每次翻译都让我着迷。非常感谢。