angular ui router directive动态ui-sref

时间:2014-04-18 13:34:11

标签: angularjs angularjs-directive angular-ui-router

HTML

<li class="dropdown signals" signals="signals" data-nb-signals="" style="visibility: hidden;">
    <a data-toggle="dropdown" href="#">
    <i class="glyphicon glyphicon-inbox"></i><span class="badge">1</span>
    <b class="caret"></b></a>
    <ul class="dropdown-menu"></ul>
</li>

JS

.directive('nbSignals',function($compile,$interpolate) {
    return {
        restrict: 'A',
        scope:{
            signals:'='
        },
        link: function(scope, element) {
            var signals = scope.signals,
            num = signals.length,
            $dropdown = element.find('ul.dropdown-menu'),
            liTpl = [],
            i18n = {add_post:'nuovo articolo'};
            if(num > 0){
                element.css('visibility', 'visible');
                for(var i = 0; i < num; i++){
                    var current = signals[i];
                    var href = $interpolate('blog_details({id:{{_id}},slug:{{slug}}})')(current);
                    liTpl.push('<li><a data-ui-sref="'+href+'">'+i18n[current.label]+'</a></li>');
                }
                $dropdown.append($compile(liTpl.join(''))(scope));
            }    

        }
    };
});

这样做我已经

  

错误:[$ parse:syntax]语法错误:令牌&#39; f9ccb520daa8c167b3431&#39;是   意外,在表达式

的第8列期待[}]

我也试过$ state.href,但我找不到方法 传递值(我不能使用href,因为给我404)

那是什么方式?

1 个答案:

答案 0 :(得分:3)

此处的例外情况来自 GUID 实际上是string。因此我们无法将其作为数字处理,即没有撇号:'

所以,因为你的电流看起来像这样:

// the one of the $scope.signals
current {
  _id: '016ab73979797971605013efc42942e8',
  ...
}

我们必须通过类似的方式更改参数,而不是:

// GUID is not a number
var href = $interpolate('blog_details({id:{{_id}},slug:{{slug}}})')(current);

我们必须使用: '{{_id}}'

// GUID is a string here
var href = $interpolate('blog_details({id:\'{{_id}}\',slug:{{slug}}})')(current);