0

I have the following markup:

<ul ng-if="item.children.length && item.open" class="fadeIn">
    <li ng-repeat="child in item.children" ng-show="child.visible">
        <a ng-href="#/{{ child.link }}">
            <span ng-bind-html="child.title"></span>
        </a>
    </li>
</ul>

The result, when the controller runs is a LI element containing a link, which contains a span, for each item in the list, which is the expected result.

The problem is that when clicking the link, nothing happens since the link is empty (#/). It is as if {{ child.link }} is not evaluated.

If i hardcode my preferred link (#/whatever), the routeProvider picks it up and it works.

I have even changed the name of the link parameter, to no avail. The scope menu structure contains a 'link' parameter for each menu.

There are no error in the console

Why isn't {{child.link}} evaluated?

sm0ke21
  • 441
  • 5
  • 20

2 Answers2

1

This is the 2nd time i have been had like this. This code is within a django template, and i have forgotten to use a verbatim tag. That means that Django will evaluate the expression to nothing before angular can have a chance to process this.

Thanks for all your help, D.

sm0ke21
  • 441
  • 5
  • 20
0

I've created a JSFIDDLE and made your code work:

<div ng-app='app' ng-controller='ctrl'>
    <ul ng-if="item.children.length && item.open" class="fadeIn">
        <li ng-repeat="child in item.children" ng-show="child.visible"> <a ng-href="#/{{ child.link }}">
            <span ng-bind-html="child.title"></span>
        </a>   
        </li>
    </ul>
</div>

angular.module('app', []).
controller('ctrl', function ($scope, $sce) {
    $scope.item = {
        open: true,
        children:[{
            visible: true,
            link: 'MyFirst',
            title: $sce.trustAsHtml('<h1>First</h1>')
        }]
    }
});

The only problem I had was with ng-bind-html. Check out this post.

Community
  • 1
  • 1
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • I can see that your fiddle works, as far as i can see it's identical to my code, which is very frustrating since this is basic stuff. There is no html in my example, so $sce is not needed, but it still isn't working. – sm0ke21 Jul 25 '15 at 13:33
  • @sm0ke21 Sometimes one uses lnk as attribute in the item and link in html. To see such a misspelling is hard. And Javascript does not show you an error because the value of link is undefined which is translated to "" by angular. – Peter Paul Kiefer Jul 25 '15 at 13:36
  • @PeterPaulKiefer i included that 2nd span with a binding to child.link and it works. I have also changed the property name to something else. the ngBind works, but ngHref still doesn't – sm0ke21 Jul 25 '15 at 13:39
  • That's strange. Did you use something like handlebars templating wich also uses {{ }} as start and begin marks for its replacements? – Peter Paul Kiefer Jul 25 '15 at 13:46
  • @PeterPaulKiefer That's exactly what's happening. I have answered my own question (further up) but i cannot accept it for another 2 days. – sm0ke21 Jul 25 '15 at 13:47
  • @sm0ke21 Sorry, I've seen your answer to late! – Peter Paul Kiefer Jul 25 '15 at 13:47