角误差我不明白?

时间:2015-06-23 06:17:59

标签: javascript angularjs angularjs-filter ng-bind-html angularjs-sce

我正在尝试构建此处的菜单:

http://jsfiddle.net/1vgcs4we/

然而,当我将其实现到我的项目中时,我收到以下错误消息:

Syntax Error: Token 'node.click' is unexpected, expecting [:] at column 3 of the expression [{{node.click}}] starting at [node.click}}].

我得到的结果是我可以看到菜单项名称,但是当我将鼠标悬停在它们上面时,我会收到一个空的菜单框?

以下是我的HTML指令:

app.directive('navMenu', ['$parse', '$compile', function ($parse, $compile) {
    return {
        restrict: 'C', //Element
        scope: true,
        link: function (scope, element, attrs) {
            scope.selectedNode = null;

            scope.$watch(attrs.menuData, function (val) {

                var template = angular.element('<ul id="parentTreeNavigation"><li ng-repeat="node in ' + attrs.menuData + '" ng-class="{active:node.active && node.active==true, \'has-dropdown\': !!node.children && node.children.length}"><a ng-href="{{node.href}}" ng-click="{{node.click}}" target="{{node.target}}" >{{node.text}}</a><sub-navigation-tree></sub-navigation-tree></li></ul>');

                var linkFunction = $compile(template);
                linkFunction(scope);
                element.html(null).append(template);

            }, true);
        }
    };
}]);
app.directive('subNavigationTree', ['$compile', function ($compile) {
    return {
        restrict: 'E', //Element
        scope: true,
        link: function (scope, element, attrs) {
            scope.tree = scope.node;

            if (scope.tree.children && scope.tree.children.length) {
                var template = angular.element('<ul class="dropdown "><li ng-repeat="node in tree.children" node-id={{node.' + attrs.nodeId + '}}  ng-class="{active:node.active && node.active==true, \'has-dropdown\': !!node.children && node.children.length}"><a ng-href="{{node.href}}" ng-click="{{node.click}}" target="{{node.target}}" ng-bind-html-unsafe="node.text"></a><sub-navigation-tree tree="node"></sub-navigation-tree></li></ul>');

                var linkFunction = $compile(template);
                linkFunction(scope);
                element.replaceWith(template);
            } else {
                element.remove();
            }
        }
    };
}]);

app.controller('HeaderController', ['$scope', '$location', function ($scope, $location) { 
    $scope.breadcrumbs = [];
    $scope.menu = [{
        text: 'HOME',
        href: '/app/index.html',
        children: [{
            text: 'MANAGE Dashboard',
            href: '/dashb'
        }]
    }, {
        text: 'MANAGE',
        href: '/manage',
        children: [{
            text: 'MANAGE PEOPLE',
            href: '/manage-people',
            children: [{
                text: 'MANAGE STAFF',
                href: '/manage-staff'
            }, {
                text: 'MANAGE CLIENTS',
                href: '/manage-clients'
            }]
        }]
    }, {
        text: 'REPORTS',
        href: '/reports',
        children: [{
            text: 'REPORT NUMERO UNO',
            href: '#'
        }, {
            text: 'REP NUMERO 2',
            href: '#',
            children: [{
                text: 'Third Tier',
                href: '#'
            }, {
                text: 'Another Third Tier',
                href: '#',
                children: [{
                    text: 'SUB SUB NAV',
                    href: '#'
                }]
            }]
        }]
    }, {
        text: 'MY INFO',
        href: '/my-info'
    }, ]


}]);

<div class="row">
<div class="large-12 columns">

   <nav class="nav-menu" menu-data="menu"></nav>

</div>
</div>

enter image description here

2 个答案:

答案 0 :(得分:2)

问题是您使用的是ng-bind-html-unsafe已被弃用的Angular 1.2+版本,您应该将其替换为ng-bind-html,然后您需要使用$sce服务使用{{}来清理该html 1}}方法。

为此,您应该编写自定义过滤器,并将该过滤器应用于您想要显示HTML的位置

$sce.trustedAsHtml

然后在你的html中,它将被用作app.filter('unsafe', function($sce) { return $sce.trustAsHtml; });

Demo here

答案 1 :(得分:0)

我通过在链接中添加{{node.text}}来修复此问题,因为没有什么我没想到检查它,因为它在小提琴上以某种方式工作。