为什么我不能在AngularJS中使用data-*作为指令的属性名?

Val*_*vel 8 javascript html5 angularjs

他的plunker你可以注意到一个奇怪的行为与data-*指令中的属性名称的模式.

电话:

<body ng-app="apptest" ng-controller="Controller">
  Test of data named attribute :
  <br/>
  <directivetest data-test="vartest" test="vartest" another-test="vartest"></directivetest>
</body>
Run Code Online (Sandbox Code Playgroud)

指令:

angular.module('apptest', [])
  .controller('Controller', ['$scope',
    function($scope) {
      $scope.vartest = "This is a test";
    }
  ])
  .directive('directivetest', function() {
    return {
      restrict: 'E',
      scope: {
        dataTest: '=',
        test: '=',
        anotherTest: '='
      },
      templateUrl: "directive.html"
    }
  });
Run Code Online (Sandbox Code Playgroud)

将考虑所有属性,directivetestdata-test因此显示:

Test of data named attribute :
Attribute with data-* name :
Attribute with regular name : This is a test
Attribute with an other regular name : This is a test
Run Code Online (Sandbox Code Playgroud)

我想知道为什么会发生这种情况(我浪费了4个小时才发现这是问题所在).

命名指令似乎是不可能的data-*,为什么呢?

我在这里阅读了一些关于它的内容http://www.w3schools.com/tags/att_global_data.asp,这是为什么我的属性未定义?浏览器根本无法读取它?

kjh*_*hes 15

data-指令的名称前缀的形式允许HTML验证器来工作,因为自定义的数据属性在HTML5遵循形式.AngularJS指令名称按如下规范化,以支持自定义数据属性:

规范化过程如下:

  • 剥离x-data-从元素/属性的前面.
  • 转换:,-_-delimited名驼峰.