2012-09-15 2 views
12

각도를 사용하고 있으며 ng-switch를 사용하여 문자열이 비어 있는지 확인하고 싶습니다. 다음 코드는 나를 위해 작동하지 않는 것 같습니다.ng-switch on empty string

<div ng-switch on="foo"> 
    <span ng-switch-when="">This string is empty</span> 
    <span ng-switch-default>This string is not empty</span> 
</div> 

이 문제에 대한 도움을 주시면 감사하겠습니다.

+0

가능한 복제 ([Angular.js NG 스위치 --- 테스트 값은 비어 있거나 미정 인 경우] http://stackoverflow.com/questions/25453689/angular-js-ng-switch-testing- if-value-is-empty-or-undefined) –

답변

11

당신은 당신의 컨트롤러에서 빈 문자열을 포함해야합니다 API를 참조 추가 참고로

function Ctrl($scope) { 
    $scope.items = ['', 'other']; 
    $scope.selection = $scope.items[0]; 
} 

을 : http://jsfiddle.net/upPgU/

+1

기본 사용 사례는 해결 방법이 필요합니다. (. – Den

2

Angular.js ng-switch ---testing if value is empty or undefined의 정의 듀프.

허용되는 답변도 거기에 적합하지 않습니다. 내 오 너무 겸손한 의견에서 가장 우아하고 명확한 솔루션은 저기에 적어도 upvotes있다.

<div ng-switch="!!selection"> 
    <div ng-switch-when="false">Empty, Null or undefined</div> 
    <div ng-switch-when="true">The string is not empty</div> 
</div>