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?

6
  • I have failed to mention that there are no errors Commented Jul 25, 2015 at 13:20
  • If the expression was not evaluated, the link would be #/{{ child.link }}. Since you have #/, it is evaluated, and the result of the evaluation is an empty string. That means the child.link is an empty string, or null, or undefined. Commented Jul 25, 2015 at 13:25
  • Can you create a plunker? recreate the problem. Commented Jul 25, 2015 at 13:25
  • I would expect that {{child.link}} is evaluated. But are you sure that, child.link has text in it. Perhaps you insert a second span bound to child.link. <span ng-bind="child.title"></span> to see if it is assigned. Commented Jul 25, 2015 at 13:26
  • I added a second <span ng-bind="child.link"> and it works. Commented Jul 25, 2015 at 13:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

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 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.
@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
That's strange. Did you use something like handlebars templating wich also uses {{ }} as start and begin marks for its replacements?
@PeterPaulKiefer That's exactly what's happening. I have answered my own question (further up) but i cannot accept it for another 2 days.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.