Angularjs directive

来自ling
跳转至: 导航搜索
app.directive("selector", ["$http", function ($http) {
    return {
        restrict: 'E',
        replace: true,
        template: "<select class='form-control'>" +
        "<option ng-repeat='selectorData in selectorDatas' value={{selectorData.key}}>{{selectorData.value}}</option>>" +
        "</select>",
        link: function (scope, element, tAttrs) {
            var ngModel = tAttrs.selectModel;
            var key = tAttrs.selectKey;
            var value = tAttrs.selectValue;
            var selectorDatas = [];
            $http.get(path + tAttrs.src).
                success(function (data, status, headers, config) {
                    if ("SUCCESS" == status && data.data != null) {
                        for (var index = 0, length = data.data.length; index < length; index++) {
                            selectorDatas.push({
                                "key": eval("data[index]." + key),
                                "value": eval("data[index]." + value)
                            })
                        }
                        scope.selectorDatas = selectorDatas;
                    }
                }).
                error(function (data, status, headers, config) {
                    alert("下拉框初始化失败:" + data);
                });
        }
    }
}]);
app.directive("sidebar", ["$state", function ($state) {
    return {
        restrict: 'E',
        replace: true,
        template: "<ul class=\"metismenu\" id=\"menu\" style='letter-spacing:1px'>\n" +
        "                <li class=\"menu\" ng-repeat='menu in menus'>\n" +
        "                    <a href=\"javascript:void(0);\" ng-click=\"go(menu.routerPath)\" style='height: 50px;position: relative;'><img style='margin-left: 40px;' ng-src='{{menu.icon}}'>\n" +
        "                        <span style='margin-left: 25px;font-weight: bold;' class=\"glyphicon\">{{menu.name}}</span>\n" +
        "                    </a>\n" +
        "                    <ul class='subMenu' ng-repeat='subMenu in menu.children'>\n" +
        "                        <li>\n" +
        "                            <a href=\"javascript:void(0);\" ng-click=\"go(subMenu.routerPath)\" class=\"sidebar-sub-item\">{{subMenu.name}}</a>\n" +
        "                            <ul>\n" +
        "                                <li ng-class=\"{'selected':currentPage.url==menuItem.routerPath}\" class='menuItem' ng-repeat='menuItem in subMenu.children'><a ng-click=\"go(menuItem.routerPath)\" href=\"javascript:void(0);\" class=\"sidebar-sub-item-children\">{{menuItem.name}}</a></li>\n" +
        "                            </ul>\n" +
        "                        </li>\n" +
        "                    </ul>\n" +
        "                </li>\n" +
        "            </ul>",
        link: function (scope, element, tAttrs) {
            scope.currentPage = currentPage = {url: null};
            scope.go = function (path) {
                if (path != null) {
                    scope.currentPage.url = path;
                    window.location.href = "console.html#/" + path;
                }
            };
        }
    }
}]);