javascript - 如何动态更改当前菜单元素AngularJS的标题(指令中的模板)

标签 javascript json angularjs controller directive

我正在考虑如何解决这个问题。让我们开始吧:

我的网站中有一个菜单栏(或导航栏)。

我从 JSON 文件创建它,我使用 AngularJS 指令和模板来创建它:

JSON 文件:

{
    "mainmenu": [
        {
            "id": "bananas",
            "title": "Bananas",
            "href": "#/bananas",
            "li-class": "menu-element"
        },
        {
            "id": "apples",
            "title": "Apples",
            "li-class": "dropdown"
            "submenu": [
                {
                    "id": "apple-lot",
                    "title": "Apples lots",
                    "href": "#/apples/lot"                  
                },
                {
                    "id": "apple-series",
                    "title": "Apples series",
                    "href": "#/apples/series"
                }               
            ]
        },
        {
            "id": "cherries",
            "title": "Cherries",
            "href": "#/cherries",
            "li-class": "menu-element"
        }
    ]
}

我的 AngularJS 指令与模板:

angular.module('dynamic-menu').directive('menuTemplate', function () {
    return {
        restrict: 'E',
        template: "<nav class=\"navbar navbar-inverse navbar-fixed-top\" role=\"navigation\" id=\"nav-bar\" style=\"margin-bottom: 0.5%\">"
                        + "<div class=\"container-fluid\">"
                            + "<div class=\"navbar-header\">"
                                + "<span class=\"navbar-brand\">TITLE</span>"
                            + "</div>"
                            + "<div class=\"collapse navbar-collapse\">"
                                + "<span class=\"navbar-brand\">"
                                        + "<ul class=\"nav navbar-nav\">"
                                            + "<li ng-repeat=\"item in mainmenu\">"
                                                + "<a href=\"{{item.href}}\">{{item.title}}</a>"
                                            +"</li>"
                                        +"</ul> <!-- /.nav navbar-nav -->"
                                    +"</span> <!-- /.navbar-brand -->"
                                +"</div> <!-- /.navbar-collapse-->"
                            +"</div> <!-- /.container-fluid-->"
                        +"</nav>"
           };
}]);

和 AngularJS Controller :

angular.module('dynamic-menu').controller('dynamicMenuCtrl', ['$scope', '$http', function ($scope, $http) {
    $http.get('MenuItems.json').success(function (data) {

        $scope.mainmenu = data;
    });

问题是 - 如何动态更改 TITLE?

我的 AngularJS 指令中的这一行:

+ "<span class=\"navbar-brand\">TITLE</span>"

也许我可以向 JSON 文件添加一些内容,并以某种方式检查是否为事件菜单元素分配了一个类? (例如 <li class="menu-item-active"> )或者如何?

这里是 Plunker - 还有一个 Title我想在我的模板中动态更改它

http://plnkr.co/edit/U1xG2E4ys7SGz7WxBtvq?p=preview

我必须使用该指令

编辑:我想要实现的目标:

关于main page我想要标题fruits ,当我在子菜单 Bananas 时我想显示标题Bananas

最佳答案

json:

{
    "title": "some title",
    "mainmenu": [
        {
            "id": "bananas",
            "title": "Bananas",
            "href": "#/bananas",
            "li-class": "menu-element"
        },
        {
            "id": "apples",
            "title": "Apples",
            "li-class": "dropdown"
            "submenu": [
                {
                    "id": "apple-lot",
                    "title": "Apples lots",
                    "href": "#/apples/lot"                  
                },
                {
                    "id": "apple-series",
                    "title": "Apples series",
                    "href": "#/apples/series"
                }               
            ]
        },
        {
            "id": "cherries",
            "title": "Cherries",
            "href": "#/cherries",
            "li-class": "menu-element"
        }
    ]
}

在模板中

"<span class=\"navbar-brand\">{{title}}</span>"

js: angular.module('dynamic-menu').controller('dynamicMenuCtrl', ['$scope', '$http',

function ($scope, $http) {
    $http.get('MenuItems.json').success(function (data) {

        $scope.mainmenu = data;
        $scope.title = data.title;
    });

http://plnkr.co/edit/LvObdZB72umVPCfTsVg1?p=preview

关于javascript - 如何动态更改当前菜单元素AngularJS的标题(指令中的模板),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31510512/

相关文章:

javascript - Snap.svg 缩放和动画 SVG

c# - JavaScript 日历问题

javascript - 为什么我的按钮在附加时会重复?

javascript - <select ng-options ="..."/> 问题

javascript - 单击时更改 radio 输入值(实际上将其更改回来)

iphone - 如何从 json 字符串中检查 null

json - 需要使用 REST - SharePoint 托管应用程序将文件夹添加到 SharePoint 2013 中的文档库

javascript - Mootools 1.3 & JSON 使用 Request.JSON 访问 json 数组中的值,并将 HTML 从 json 数据插入 DOM

ios - <ion-footer-bar> 在 iOS Ionic View 中显示灰色细线

javascript - 如何更新每个响应的 Ck 编辑器内容