Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular "=" scope does not work with camelCase

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 )

like image 213
kanitw Avatar asked Aug 21 '13 04:08

kanitw


1 Answers

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. :)

like image 87
zs2020 Avatar answered Sep 22 '22 14:09

zs2020