Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using AngularJS ng-bind and ng-href together?

Tags:

angularjs

Using AngularJS, I've noticed a pattern that seems wrong to me.

When building a table, my data is bound using ng-bind. But if I need the text in the cell to link to something, the link has to get created manually.

A non-linked table cell looks like:

<td ng-bind="customer.name"></td>

But if I want to create a link, I do:

<td><a ng-href="/customer/{{customer.id}}">{{customer.name}}</a></td>

Is there a way to create the link using attributes? Something like:

<td ng-bind="customer.name" ng-href="/customer/{customer.id}"></td>
like image 245
pmn Avatar asked Jul 24 '12 16:07

pmn


People also ask

How do I bind a href in AngularJS?

The ng-href directive overrides the original href attribute of an <a> element. The ng-href directive should be used instead of href if you have AngularJS code inside the href value. The ng-href directive makes sure the link is not broken even if the user clicks the link before AngularJS has evaluated the code.

Does ng-bind bind the application data to HTML tags in AngularJS?

ng-bind directive binds the AngularJS Application data to HTML tags. ng-bind updates the model created by ng-model directive to be displayed in the html tag whenever user input something in the control or updates the html control's data when model data is updated by controller.

What is the difference between Ng-bind and NG-model in AngularJS?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.

Does href work in angular?

href allows users to visit an external website from the current website. It is mostly used in anchor tags. Anchor tags are the most common HTML tags that take href as a parameter and route users to another website. In Angular, developers should use ng-href instead of href in the anchor tags.


1 Answers

This isn't really an AngularJS issue; this is more about how HTML works. HTML doesn't allow you to just add an href attribute to any element to create a link. You need to use the anchor tag to create a link.

If you want, you could write a directive that produces the anchor tag inside cells. But that hardly seems worth it, and most would probably agree that it produces less semantic, more confusing markup.

HTML links spec: http://www.w3.org/TR/html4/struct/links.html

like image 129
btford Avatar answered Nov 15 '22 07:11

btford