ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [angular2] 토글버튼을 이용하여 상태값 바꾸기
    코딩/angular 2019. 9. 17. 15:46

    토글버튼은 인터넷에 찾아면 많지만 angular2에서 토글 버튼 값을 이용하는 것이 안나와 있어서 글을 써봅니다.

     

    일단 토글키를 만들어야 하는데 저는

    https://www.htmllion.com/css3-toggle-switch-button.html

    여기를 참조 하였습니다.

    html 에 삽입하고

    <label class="switch">
    
        <input class="switch-input" type="checkbox" [(ngModel)]="isOpen"  (click)="changeOpen()" />
    
        <span class="switch-label" data-on="오픈" data-off="준비 중" ></span> 
    
        <span class="switch-handle"></span> 
    
    </label>

    css 로 꾸며주면

     

    .switch {
    	position: relative;
    	display: inline-block;
    	
    	width: 100px;
    	height: 30px;
    	padding: 3px;
    	margin: 0 10px 10px 0;
    	background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px);
    	background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px);
    	border-radius: 18px;
    	box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05);
    	cursor: pointer;
        box-sizing:content-box;
        top: 5px;
    }
    .switch-input {
    	position: absolute;
    	top: 0;
    	left: 0;
    	opacity: 0;
    	box-sizing:content-box;
    }
    .switch-label {
    	position: relative;
    	display: block;
    	height: inherit;
    	font-size: 15px;
    	text-transform: uppercase;
    	background: #eceeef;
    	border-radius: inherit;
    	box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15);
    	box-sizing:content-box;
    }
    .switch-label:before, .switch-label:after {
    	position: absolute;
    	top: 50%;
    	margin-top: -.5em;
    	line-height: 1;
    	-webkit-transition: inherit;
    	-moz-transition: inherit;
    	-o-transition: inherit;
    	transition: inherit;
    	box-sizing:content-box;
    }
    .switch-label:before {
    	content: attr(data-off);
    	right: 11px;
    	color: #aaaaaa;
    	text-shadow: 0 1px rgba(255, 255, 255, 0.5);
    }
    .switch-label:after {
    	content: attr(data-on);
    	left: 11px;
    	color: #FFFFFF;
    	text-shadow: 0 1px rgba(0, 0, 0, 0.2);
    	opacity: 0;
    }
    .switch-input:checked ~ .switch-label {
    	background: #2d429b;
    	box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2);
    }
    .switch-input:checked ~ .switch-label:before {
    	opacity: 0;
    }
    .switch-input:checked ~ .switch-label:after {
    	opacity: 1;
    }
    .switch-handle {
    	position: absolute;
    	top: 4px;
    	left: 4px;
    	width: 28px;
    	height: 28px;
    	background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0);
    	background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0);
    	border-radius: 100%;
    	box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
    }
    .switch-handle:before {
    	content: "";
    	position: absolute;
    	top: 50%;
    	left: 50%;
    	margin: -6px 0 0 -6px;
    	width: 12px;
    	height: 12px;
    	background: linear-gradient(to bottom, #eeeeee, #FFFFFF);
    	background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF);
    	border-radius: 6px;
    	box-shadow: inset 0 1px rgba(0, 0, 0, 0.02);
    }
    .switch-input:checked ~ .switch-handle {
    	left: 74px;
    	box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2);
    }
     
    /* Transition
    ========================== */
    .switch-label, .switch-handle {
    	transition: All 0.3s ease;
    	-webkit-transition: All 0.3s ease;
    	-moz-transition: All 0.3s ease;
    	-o-transition: All 0.3s ease;
    }

    됩니다.

     

    이런 토글버튼이 생성이 됩니다.

     

    이제 토글이 어떤값을 나오는지 봐야하는데

    input 에 type이 checkbox 인것을 보고 true / false 로 구분 지을 수 있겠구나 라고 생각했습니다.

    그래서 [(ngModel)] 을 사용하였고 클릭시 마다 그 값을 나오게 하였습니다.

    결과는 오픈 일때 버튼을 누르면 true값이 반환되고 준비 중 일떄 버튼을 누르면 false 로 값이 반환됩니다.

     

    하지만 저는 오픈일때 값을 true 준비중일떄의 값을 false 로 결정 하였기 때문에

    값을 사용할때에는 !this.isOpen 이라고 해서 값을 제가 결정한대로 반환을 받아 사용 하였습니다.

     

    -------------------------------------------------------------------------------------------------------------

     

    (click)="" 으로 받아서 위의 값이 들어오는것을 확인하였습니다

    (change) 로 받으니 오픈할때 true 준비중일 때 false로 값이 들어오는것을 확인하여 수정 하였습니다.

    '코딩 > angular' 카테고리의 다른 글

    ngClass로 페이징 번호 효과내기  (0) 2019.08.09
Designed by Tistory.