Angular “=” scope does not work with camelCase

送分小仙女□ 提交于 2019-12-06 00:52:46

问题


I'm scope property of a directive

It works fine when I use show as attr name.

<span ng-repeat="field in fields">
  <field-pill field="field" show="true"></field-pill>
</span>

app.js

angular.module('app',[]);

angular.module('app')
  .controller('AppCtrl', function($scope){
      $scope.fields = [1,2,3,4];
    });

angular.module('app')
  .directive('fieldPill', function () {
    return {
      template: '<div class="pill">{{field}}:{{show}}--<span ng-show="show">x</span></div>',
      restrict: 'E',
      scope:{
        field: "=",
        "show": "="
      }
    };
  });

(See this plunkr http://plnkr.co/edit/AcqmxeCerCOtGaw9dq9t?p=preview)

But the directive doesn't load the boolean data at all when I use x-show as the attribute name.

<span ng-repeat="field in fields">
  <field-pill field="field" x-show="true"></field-pill>    
</span>

app.js

angular.module('app',[]);

angular.module('app')
  .controller('AppCtrl', function($scope){
      $scope.fields = [1,2,3,4];
    });

angular.module('app')
  .directive('fieldPill', function () {
    return {
      template: '<div class="pill">{{field}}:{{xShow}}--<span ng-show="xShow">x</span></div>',
      restrict: 'E',
      scope:{
        field: "=",
        xShow: "="
      }
    };
  });

Can anyone explain why?

(See this plunkr for the code with x-show http://plnkr.co/edit/2txoY3VaShH6WggnugcE?p=preview )


回答1:


I think it is relating to the x- prefix. If you change it to anything like mShow, m-show, it will work.

From the HTML5 spec:

Attribute names beginning with the two characters "x-" are reserved for user agent use and are guaranteed to never be formally added to the HTML language. For flexibility, attributes names containing underscores (the U+005F LOW LINE character) are also reserved for experimental purposes and are guaranteed to never be formally added to the HTML language.

So avoid using x- for normal attribute name. :)



来源:https://stackoverflow.com/questions/18348692/angular-scope-does-not-work-with-camelcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!