angular2之ngClass

1.页面

<div class="folder-box" [hidden]="isHidden" *ngIf="isAuthenticated()" id="mainfolder"> <!--如果不需要折叠菜单 直接给该处添加[hidden]="true"-->

<div class="folder-menu" [hidden]="hiddenMemu">

<div *ngFor="let secM of secMenuList,let firstIndex= index;">

<div class="first-class" [ngClass]="{‘this‘: firstIndex==firstClass}" (click)="showOrOpen(secM,firstIndex)">

<i>

<img src="../../../content/images/icon01.png"/>

</i>

<span>{{secM.label}}</span>

<i class="folder unfolder">

<img src="../../../content/images/icon03.png" *ngIf="secM.children.length"/>

</i>

</div>

<div class="submenu" *ngIf="secM.children.length" [hidden]="secM.showFlag" >

<div *ngFor="let thrM of secM.children,let secondIndex = index;">

<div class="second-class" [ngClass]="{‘this‘: secondIndex == secondClass}" (click)="openWin(thrM,secondIndex)">

<span>{{thrM.label}}</span>

</div>

</div>

</div>

</div>

</div>

<!--有三级级菜单-->

<!--只有二级菜单-->

<div class="show" [ngClass]="divStyle" (click)="hiddenMemuDiv()">

<img src="../../../content/images/icon06.png"/>

</div>

</div>

2.ts组件

import { Component, OnInit ,OnDestroy} from ‘@angular/core‘;

import { Router, ActivatedRouteSnapshot, NavigationEnd, RoutesRecognized } from ‘@angular/router‘;

import { EventManager, AlertService } from ‘ng-jhipster‘;

import { Subscription } from ‘rxjs/Rx‘;

import { Title } from ‘@angular/platform-browser‘;

import { Principal, StateStorageService } from ‘../../shared‘;

@Component({

selector: ‘jhi-main‘,

templateUrl: ‘./main.component.html‘,

styleUrls: [

‘main.css‘

]

})

export class JhiMainComponent implements OnInit, OnDestroy {

isHidden: boolean;

isSidebarCollapsed: boolean;

hiddenMemu: boolean = false;

divStyle:any;

eventSubscriber: Subscription;

secMenuList: any;

firstClass:number = 0;

secondClass:number = -1;

constructor(

private titleService: Title,

private router: Router,

private principal: Principal,

private $storageService: StateStorageService,

private eventManager: EventManager // 事件管理

) {

this.isHidden = true;

this.secMenuList = [];

}

private getPageTitle(routeSnapshot: ActivatedRouteSnapshot) {

let title: string = (routeSnapshot.data && routeSnapshot.data[‘pageTitle‘]) ? routeSnapshot.data[‘pageTitle‘] : ‘schintacloudApp‘;

if (routeSnapshot.firstChild) {

title = this.getPageTitle(routeSnapshot.firstChild) || title;

}

return title;

}

hiddenMemuDiv(){

if( this.hiddenMemu ==false){

this.hiddenMemu = true;

this.divStyle = "hide";

} else {

this.hiddenMemu = false;

this.divStyle = "show";

}

}

ngOnInit() {

this.eventSubscriber = this.eventManager.subscribe(‘menuChange‘, (response) => this.changeMenu(response));

this.router.events.subscribe((event) => {

if (event instanceof NavigationEnd) {

this.titleService.setTitle(this.getPageTitle(this.router.routerState.snapshot.root));

}

if (event instanceof RoutesRecognized) {

let params = {};

let destinationData = {};

let destinationName = ‘‘;

let destinationEvent = event.state.root.firstChild.children[0];

if (destinationEvent !== undefined) {

params = destinationEvent.params;

destinationData = destinationEvent.data;

destinationName = destinationEvent.url[0] ? destinationEvent.url[0].path : ‘‘;

}

let from = {name: this.router.url.slice(1)};

let destination = {name: destinationName, data: destinationData};

this.$storageService.storeDestinationState(destination, params, from);

}

});

}

changeDetroy(event){

if(event && event !=null && event ==‘navbarNgOnDestroy‘) {

this.isHidden = true;

}

}

ngOnDestroy() {

this.eventManager.destroy(this.eventSubscriber);

}

isAuthenticated() {

return this.principal.isAuthenticated();

}

toggleSidebar() {

this.isSidebarCollapsed = !this.isSidebarCollapsed;

}

changeMenu(data) {

if (data.content) {

if (data.content.sign == ‘1‘) {

this.secMenuList = data.content.res;

this.showOrOpen(this.secMenuList[0]);

this.isHidden = false;

} else if (data.content.sign == ‘2‘) {

this.showOrOpen(data.content.res);

this.isHidden = true;

} else if (data.content.sign == ‘3‘) {

this.secMenuList = data.content.res;

this.isHidden = false;

} else if (data.content.sign == ‘4‘) {

this.isHidden = true;

}

} else {

this.router.navigate([‘/‘]);

this.isHidden = true;

}

}

showOrOpen(secM,firstIndex?:any) {

if(firstIndex || firstIndex == 0) {

this.firstClass = firstIndex;

} else {

this.firstClass = 0;

}

if (secM && secM.children.length > 0) {

secM.showFlag = !secM.showFlag;

} else {

this.openWin(secM);

}

}

openWin(obj,secondIndex?:any) {

if(secondIndex || secondIndex == 0) {

this.firstClass = -1;

this.secondClass = secondIndex;

} else {

this.secondClass = -1;

}

if (obj.data) {

this.router.navigate([‘/‘+obj.data.menuIdPath]);

}

}

}

3.css文件

/* ==========================================================================

Main

========================================================================== */

.layout-sidebar{

width: 260px;

height: 100%;

position: fixed;

z-index: 102;

overflow-y: auto;

transition: margin-left .3s;

background: #4e5159;

background: -webkit-gradient(linear,left top,left bottom,color-stop(0,#4e5159),color-stop(100%,#1f2023));

background: -webkit-linear-gradient(top,#4e5159,#1f2023);

background: linear-gradient(180deg,#4e5159 0,#1f2023);

}

.main-content{

/*margin-left: 260px;*/

/*padding-top: 50px;*/

transition: margin-left .3s;

/*padding-bottom: 40px;*/

padding: 0;

padding-right: 0rem;

}

.folder-box{

display: flex;

background: #4c545f;

flex-direction: column;

position: relative;

box-shadow: 0 0 10px #6d6d6d;

-webkit-box-shadow: 0 0 10px #6d6d6d;

-moz-box-shadow: 0 0 10px #6d6d6d;

-ms-box-shadow: 0 0 10px #6d6d6d;

-o-box-shadow: 0 0 10px #6d6d6d;

margin-right: 1rem;

}

.folder-menu{

display: flex;

flex-direction: column;

overflow-y: scroll;

width: 13rem;

}

.folder-menu::-webkit-scrollbar{

width: 6px;

height: 6px;

background-color: transparent;

}

/*定义滚动条的轨道,内阴影及圆角*/

.folder-menu::-webkit-scrollbar-track{

border-radius: 5px;

background-color: transparent;

}

/*定义滑块,内阴影及圆角*/

.folder-menu::-webkit-scrollbar-thumb{

/*width: 10px;*/

height: 10px;

border-radius: 5px;

-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);

background-color: #4c545f;

}

.folder-menu .first-class{

display: flex;

width: 100%;

height: 3rem;

line-height: 3rem;

border-bottom: 1px solid #5F5D5D;

cursor: pointer;

}

.folder-menu .first-class:hover{

background: #6c737d;

}

.folder-menu .first-class.this{

background-image: url(../../../content/images/icon05.png);

background-position: right center;

background-repeat: no-repeat;

background-color: #6c737d;

}

.folder-menu .first-class i{

display: -webkit-box;

width: 3rem;

height: 3rem;

-webkit-box-pack: center;

justify-content: center;

}

.folder-menu .first-class i img{

height: 18px;

width: auto;

}

.folder-menu .first-class span{

display: -webkit-box;

display: -moz-box;

display: -ms-flexbox;

display: -o-box;

display: box;

-webkit-box-flex: 1;

-moz-box-flex: 1;

-ms-flex: 1;

-o-box-flex: 1;

box-flex: 1;

color: #fff;

/*font-weight: bold;*/

-webkit-box-align: center;

width: 7rem;

/*padding-left: 1rem;*/

}

.folder-menu .first-class i{

display: -webkit-box;

width: 3rem;

height: 3rem;

-webkit-box-pack: center;

justify-content: center;

}

.folder-menu .first-class i.folder{

width: 2rem;

}

.folder-menu .first-class i.folder img{

height: 10px;

width: auto;

}

.folder-menu .first-class i.unfolder img{

-webkit-transform: rotate(-90deg);

transform: rotate(-90deg);

-moz-transform: rotate(-90deg);

-ms-transform: rotate(-90deg);

-o-transform: rotate(-90deg);

}

.folder-menu .submenu{

display: -webkit-box;

display: box;

-webkit-box-flex: 1;

-moz-box-flex: 1;

-ms-box-flex: 1;

-moz-box-flex: 1;

-webkit-box-orient: vertical;

-moz-box-orient: vertical;

-ms-box-orient: vertical;

}

.folder-menu .submenu .second-class{

display: flex;

width: 100%;

height: 3rem;

line-height: 3rem;

margin-bottom: 1px;

/**/

cursor: pointer;

}

.folder-menu .submenu .second-class:hover{

background-color: #05C9D9;

}

.folder-menu .submenu .second-class.this{

background-image: url(../../../content/images/icon05.png);

background-position: right center;

background-repeat: no-repeat;

background-color: #6c737d;

}

.folder-menu .submenu .second-class span{

display: -webkit-box;

display: box;

-webkit-box-flex: 1;

-moz-box-flex: 1;

-ms-box-flex: 1;

-moz-box-flex: 1;

color: #fff;

-webkit-box-align: center;

width: 100%;

padding-left: 4.5rem;

background: url(../../../content/images/icon04.png) 3rem center no-repeat;

}

.show{

display: -webkit-box;

position: absolute;

top: 50%;

left: 13rem;

width: 20px;

height: 40px;

background: #3F4752;

-webkit-box-align: center;

-ms-box-align: center;

-moz-box-align: center;

-o-box-align: center;

-webkit-box-pack: center;

-ms-box-pack: center;

-moz-box-pack: center;

-o-box-pack: center;

margin-top: -10px;

z-index: 99;

cursor: pointer;

-webkit-transform:rotate(180deg);

transform:rotate(180deg);

-moz-transform:rotate(180deg);

-o-transform:rotate(180deg);

-ms-transform:rotate(180deg);

}

.hide{

display: -webkit-box;

position: fixed;

top: 50%;

left: 0rem;

width: 20px;

height: 40px;

background: #3F4752;

-webkit-box-align: center;

-ms-box-align: center;

-moz-box-align: center;

-o-box-align: center;

-webkit-box-pack: center;

-ms-box-pack: center;

-moz-box-pack: center;

-o-box-pack: center;

margin-top: -10px;

z-index: 99;

cursor: pointer;

-webkit-transform:rotate(0deg);

transform:rotate(0deg);

-moz-transform:rotate(0deg);

-o-transform:rotate(0deg);

-ms-transform:rotate(0deg);

}

/*

.navbar{

position: fixed;

// left: 260px;

right: 0;

top: 0;

z-index: 102;

padding: 0;

height: 50px;

background: #0388e5;

transition: left .3s;

color: #fff;

padding: 9px 20px;

line-height: 1.15;

}*/

.navbar{

padding: 0;

display: initial !important;

}

:host >>> .footer {

border-top: 1px solid rgba(0,0,0,.125);

/*margin-left: 260px;*/

transition: margin-left .3s;

}

.sidebar-collapsed .layout-sidebar{

margin-left: -260px;

}

.sidebar-collapsed .main-content{

margin-left: 0;

}

.sidebar-collapsed .navbar{

left: 0;

}

:host >>> .sidebar-collapsed .footer{

margin-left: 0;

}

时间: 2024-11-07 20:34:27

angular2之ngClass的相关文章

Angular2 架构

1. 说明 Angular 2 是一个用 HTML 和 JavaScript (或者可以编译成JavaScript)来构建应用程序的框架.该框架包含了一系列的库. 在 Angular 里,我们这样来构建应用程序:用带 Angular 扩展语法的 HTML 写模板 ,用 组件 类管理这些模板,用 服务 添加应用逻辑, 用 根组件 完成 Angular 启动 . 首先,我们来看一些整体的结构图,这个图来自于官方网站. 通过图可以看出,Angular 应用中的 8 个主要部分: l 模块 (Modul

[Angular 2] ng-class and Encapsulated Component Styles

import {Input, Component, View, NgClass} from "angular2/angular2"; @Component({ selector: 'todo-item-render' }) @View({ directives: [NgClass], styles: [` .started{ color: green; } .completed { text-decoration: line-through; } `], template: ` <

Angular2版本更新

2.0.0-beta.0 somnambulant-inauguration (2015-12-15) Enjoy! 2.0.0-alpha.55 (2015-12-15) Bug Fixes router: export ROUTER_LINK_DSL_PROVIDER and hide MockPopStateEvent (fc75220) Features core: enable dev mode by default (3dca9d5) BREAKING CHANGES Before

Angular2 入门详解

AngularJS 2 快速入门 npm是什么?   npm其实是Node.js Package Manager的简称,是Node.js包管理工具(package manager) 安装Node.js之后配置npm: 配置npm的全局模块的存放路径以及cache的路径: 查看全局模块路径:npm config get prefix 查看全局cache路径:npm config get cache 在NodeJs下建立"node_global"及"node_cache"

Angular2 初学小记

1.与Angular1的异同 几乎完全不同(什么鬼~ 1)保留一些特性 表达式仍旧用{{}}. 2)属性指令变为驼峰式 ng-if ---> ngIf 3)ng-repeat被ngFor代替 4)ng-model ------> [(ngModule)] 注意符号 5)Angular2中,自带原始指令要加哈希前缀 # 6)Angular2新增了对移动端的设计 7)Angular2摒弃了1中的核心:$scope 8)Angular2使用zone.js来检测变化. 9)新增组件类component

Angular2 指令

1. 指令说明 Angular2 指令是构成Angular2应用程序的重要组成部分,指令主要用来对模板的标签或者元素附加一些新的特性或者功能,改变一个 DOM 元素的外观或行为,Angular2指令和组件十分类似,也有由模块,注解,元数据以及组件类组成,实际上组件继承于指令,不同的是指令没有模板的信息,主要存在两种类型的指令: l 结构型指令:会通过添加 / 删除 DOM 元素来更改 DOM 树布局 l 属性型指令:指令改变一个元素的外观或行为. 2. 结构型指令 结构性指令一般都使用的是内置的

angular2.x 下拉多选框选择组件

angular2.x - 5.x 的下拉多选框选择组件 ng2 -- ng5.最近在学angular4,经常在交流群看见很多人问 下拉多选怎么做... 今天就随便写的个. 组件源码 百度云   链接:https://pan.baidu.com/s/1dEHwKmt 密码: mhta 下面贴代码: 界面 引用  selectList  是 下拉框的数据列表 redSelList() 方法是获取 选择完成后的数据 <app-select-checkbox [itemList]="selectL

angularJs中关于ng-class的三种使用方式说明

在开发中我们通常会遇到一种需求:一个元素在不同的状态需要展现不同的样子. 而在这所谓的样子当然就是改变其css的属性,而实现能动态的改变其属性值,必然只能是更换其class属性 这里有三种方法: 第一种:通过数据的双向绑定(不推荐) 第二种:通过对象数组 第三种:通过key/value 下面简单说下这三种: 第一种:通过数据的双向绑定 实现方式: function changeClass(){   $scope.className = "change2"; } <div clas

angular2开发01

angular2开发01 1. angular2 开发准备 1.1. 安装node 1.2. 安装npm 1.3. 运行qickStart 1 angular2 开发准备 开发环境是linux mint 17.3 64位 1.1 安装node node在linux的部署主要有三种方式,第一种方式,使用apt-get install nodejs安装,这种方式有缺点,安装后的版本太低(0.10),官网都已经到4.5了; 第二种就是自己下载源码,手动编译二进制,这种方式要求有点高,属于高级用 户方式