Can I use another prefix instead of `ng` with angularjs?
Asked Answered
B

1

22

I'm newbie to angularjs. When I read the docs, I found it uses ng as prefix of attributes:

<body ng:controller="PhoneListCtrl">
  <ul>
    <li ng:repeat="phone in phones">
      {{phone.name}}
      <p>{{phone.snippet}}</p>
    </li>
  </ul>
</body>

I want to know if I can modify it as another word, such as x? Since I think x is much easier to type than ng.

Begga answered 17/3, 2012 at 3:8 Comment(1)
You could fork it, and 'ng' with 'x' replace, then do git rebase ;-)Orthopterous
P
26

Since v1.0.0rc1, these are all equivalent:

<div ng-show="isVisible">Using ng-show</div>
<div ng:show="isVisible">Using ng:show</div>
<div data-ng-show="isVisible">Using data-ng-show</div>
<div x-ng-show="isVisible">Using x-ng-show</div>
<div class="ng-show:isVisible">Using class="ng-show:isVisible"</div>

Here's working fiddle: http://jsfiddle.net/vojtajina/Fgf3Q/

However, the main reason behind this was allowing valid html. So, you ca use x-* prefix for your custom directives, but not for Angular ones.

Check out docs for more info.

Provincetown answered 17/3, 2012 at 20:7 Comment(7)
Although this is not exactly what I want -- I hope to use x-show instead of ng-show, but I learn more about angularjs, thank you!Begga
I kind of expected x:* to work (after setting xmlns:x="angularjs.org"), but it doesn't...Wicker
Does data-x-mydirective work? (x-mydirective is not valid HTML5?)Orthopterous
data-mydirective works and is valid html5, why would you need data-x-mydirective ?Provincetown
very helpful, thanks! this also addresses some of the concerns w/ Angular not using valid HTML (using data- instead of ng- directives)Vitric
That said, I think somebody should create a better html validator that can understand Angular stuff. Using data-* makes the templates awfull and only causes to "skip" these attributes as valid. Would be better to actually validate even the Angular attributes and produce errors like "you misspelled ng-rrepeat". It's not simple, but somebody should try it ;-)Provincetown
Multiple personalities much? I like the class and data syntaxes but the others just add confusion :-(Rombert

© 2022 - 2024 — McMap. All rights reserved.