angularjs - Angular ui 路由器指令动态 ui-sref

标签 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));
            }    

        }
    };
});

这样做我已经

Error: [$parse:syntax] Syntax Error: Token 'f9ccb520daa8c167b3431' is unexpected, expecting [}] at column 8 of the expression

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

那有什么办法呢?

最佳答案

这里的异常(exception)是 GUID 实际上是一个字符串。因此我们不能将其作为数字处理,即不带撇号:'

所以,因为您的电流可能如下所示:

// 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);

关于angularjs - Angular ui 路由器指令动态 ui-sref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155279/

相关文章:

angularjs - 如何在firebase中使用相同的方法同时更新多个对象中的数据?

javascript - 快速路线内的 Sequelize - 包括模型匹配搜索词的地方

javascript - 与自定义指令范围的 2 种方式数据绑定(bind)

angular - 当重定向到带有 SAML 响应的回调 URL 时出现错误无法以 Angular 发布

javascript - Angular UI 路由器 : Different states with same URL?

javascript - ng-toggle,添加到数组,

javascript - 如何在 AngularJS 模板中调用数据函数?

javascript - 自定义指令 : How evaluate bindings with dynamic HTML

javascript - Angularjs 中的自定义验证器 - $validators 未显示在链接函数中

javascript - 用户界面路由器 : Difference between onEnter and onStart?