조건식을 사용한 AngularJS ng-style
저는 제 문제를 다음과 같이 처리하고 있습니다.
ng-style="{ width: getTheValue() }"
그러나 컨트롤러 측에서 이 기능을 사용하지 않도록 하기 위해 다음과 같은 작업을 수행하는 것이 좋습니다.
ng-style="{ width: myObject.value == 'ok' ? '100%' : '0%' }"
이거 어떻게 해?
간단한 예:
<div ng-style="isTrue && {'background-color':'green'} || {'background-color': 'blue'}" style="width:200px;height:100px;border:1px solid gray;"></div>
{'background-color':'green'} RETURN True
또는 같은 결과:
<div ng-style="isTrue && {'background-color':'green'}" style="width:200px;height:100px;border:1px solid gray;background-color: blue"></div>
기타 조건부 가능성:
<div ng-style="count === 0 && {'background-color':'green'} || count === 1 && {'background-color':'yellow'}" style="width:200px;height:100px;border:1px solid gray;background-color: blue"></div>
@Yoshi의 말대로 각도 1.1.5부터 변화 없이 사용할 수 있습니다.
angular < 1.1.5 를 사용하는 경우는, ng-class 를 사용할 수 있습니다.
.largeWidth {
width: 100%;
}
.smallWidth {
width: 0%;
}
// [...]
ng-class="{largeWidth: myVar == 'ok', smallWidth: myVar != 'ok'}"
다음과 같이 달성할 수 있습니다.
ng-style="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
@jfredsilva는 이 질문에 대한 가장 간단한 답을 가지고 있습니다.
ng-style="{'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
하지만 좀 더 복잡한 문제에 대해서는 제 답변을 고려해 보는 것이 좋을 것 같습니다.
삼진법과 같은 예:
<p ng-style="{width: {true:'100%',false:'0%'}[myObject.value == 'ok']}"></p>
좀 더 복잡한 것:
<p ng-style="{
color: {blueish: 'blue', greenish: 'green'}[ color ],
'font-size': {0: '12px', 1: '18px', 2: '26px'}[ zoom ]
}">Test</p>
한다면$scope.color == 'blueish'
의 색상은 '파란색'이 됩니다.
한다면$scope.zoom == 2
폰트 사이즈는 26px 입니다.
angular.module('app',[]);
function MyCtrl($scope) {
$scope.color = 'blueish';
$scope.zoom = 2;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl" ng-style="{
color: {blueish: 'blue', greenish: 'green'}[ color ],
'font-size': {0: '12px', 1: '18px', 2: '26px'}[ zoom ]
}">
color = {{color}}<br>
zoom = {{zoom}}
</div>
expression과 함께 사용하는 경우 다음과 같은 방법이 있습니다.
<span class="ng-style: yourCondition && {color:'red'};">Sample Text</span>
그러나 가장 좋은 방법은 ng-class를 사용하는 것입니다.
일반적인 메모에서는 다음 조합을 사용할 수 있습니다.ng-if
그리고.ng-style
조건 변경과 배경 이미지 변경을 통합합니다.
<span ng-if="selectedItem==item.id"
ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_active.png)','background-size':'52px 57px','padding-top':'70px','background-repeat':'no-repeat','background-position': 'center'}"></span>
<span ng-if="selectedItem!=item.id"
ng-style="{'background-image':'url(../images/'+'{{item.id}}'+'_deactivated.png)','background-size':'52px 57px','padding-top':'70px','background-repeat':'no-repeat','background-position': 'center'}"></span>
단일 css 속성의 경우
ng-style="1==1 && {'color':'red'}"
복수의 css 속성에 대해서는, 이하를 참조해 주세요.
ng-style="1==1 && {'color':'red','font-style': 'italic'}"
1==1을 조건식으로 대체합니다.
3진 연산자에 대한 다음 구문도 작동합니다.
ng-style="<$scope.var><condition> ? {
'<css-prop-1>':((<value>) / (<value2>)*100)+'%',
'<css-prop-2>':'<string>'
} : {
'<css-prop-1>':'<string>',
'<css-prop-2>':'<string>'
}"
어디에<value>
$120 속성 값입니다.예:
ng-style="column.histograms.value=>0 ?
{
'width':((column.histograms.value) / (column.histograms.limit)*100)+'%',
'background':'#F03040'
} : {
'width':'1px',
'background':'#2E92FA'
}"
```
이를 통해 css 속성 값으로 어느 정도 계산할 수 있습니다.
복수의 독립된 조건에 대해서, 이하와 같이 하고 있습니다.매력적으로 동작합니다.
<div ng-style="{{valueFromJS}} === 'Hello' ? {'color': 'red'} : {'color': ''} && valueFromNG-Repeat === '{{dayOfToday}}' ? {'font-weight': 'bold'} : {'font-weight': 'normal'}"></div>
편집:
네, 전에는 몰랐습니다.AngularJS
통상은 을 가리킨다Angular
v1 버전 및 Angular-to-Angular v2+만
이 답변은 다음에만 적용됩니다.Angular
나중에 참조할 수 있도록 여기에 남겨두겠습니다.
어떻게 동작하는지는 모르겠지만 Angular 9에서는 다음과 같이 ngStyle을 괄호로 묶어야 합니다.
[ng-style]="{ 'width' : (myObject.value == 'ok') ? '100%' : '0%' }"
그렇지 않으면 작동하지 않습니다.
스타일을 추가하기 위해 ng-class를 사용하고 있습니다.
ng-class="column.label=='Description' ? 'tableStyle':
column.label == 'Markdown Type' ? 'Mtype' :
column.label == 'Coupon Number' ? 'couponNur' : ''
"
스타일을 부여하기 위해 angular.js의 ng 클래스 지시어와 함께 3진 연산자를 사용합니다.그런 다음 .css 또는 .scss 파일에서 클래스의 스타일을 정의합니다.예:-
.Mtype{
width: 90px !important;
min-width: 90px !important;
max-width: 90px !important;
}
.tableStyle{
width: 129px !important;
min-width: 129px !important;
max-width: 129px !important;
}
.couponNur{
width: 250px !important;
min-width: 250px !important;
max-width: 250px !important;
}
언급URL : https://stackoverflow.com/questions/19375695/angularjs-ng-style-with-a-conditional-expression
'itsource' 카테고리의 다른 글
Ionic: 어떻게 div의 중심을 잡습니까? (0) | 2023.02.14 |
---|---|
JSX/TSX에서 TypeScript 캐스트를 사용하는 방법 (0) | 2023.02.14 |
변경 후 각도 JS 업데이트 입력 필드 (0) | 2023.02.14 |
ng-pattern을 사용하여 angularJs의 이메일 ID를 확인하는 방법 (0) | 2023.02.14 |
응답 텍스트 파일을 읽는 방법 (0) | 2023.02.14 |