【问题标题】:Angular error i dont understand?我不明白的角度错误?
【发布时间】:2015-06-23 06:17:58
【问题描述】:

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

http://jsfiddle.net/1vgcs4we/

但是,当我将它实施到我的项目中时,我收到以下错误消息:

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

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

这也是我的 2 个带有 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>

【问题讨论】:

  • 它在我这边正常工作..意味着在菜单中呈现菜单选项
  • @pankajparkar 请参考我刚刚上传的附图
  • 项目是空白的,但我可以点击它们?
  • 但在你的小提琴中它正确显示它,,
  • 我对指令上的车把{{}} 有点困惑。像 ng-click 或 ng-href 这样的 Afaik 指令不需要那些把手。所以如果node上有点击功能,也许你应该试试ng-click="node.click()"。同样使用 {{node.click}} 您实际上不会在节点点击时 任何事情。表达式只会被评估

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


【解决方案1】:

问题是您使用的 ng-bind-html-unsafe 已从 Angular 1.2+ 中弃用,您应该将其替换为 ng-bind-html 然后您需要使用 $sce 服务使用 $sce.trustedAsHtml 方法清理该 html。

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

app.filter('unsafe', function($sce) { 
    return $sce.trustAsHtml; 
});

然后在你的 html 中它会被用作ng-bind-html="node.text| unsafe"

Demo here

【讨论】:

【解决方案2】:

我通过在链接中添加一个 {{node.text}} 解决了这个问题,因为那里没有任何东西我没想过要检查它,因为它以某种方式在小提琴上工作。

【讨论】:

  • 它在小提琴上工作,因为您使用的是 1.1 版本,ng-bind-html-unsafe 正在工作,请查看最低答案了解详细信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 2019-07-26
  • 1970-01-01
  • 2010-11-21
  • 2019-03-13
  • 1970-01-01
相关资源
最近更新 更多