2012-09-15 9 views
12

Używam Angular i chcę sprawdzić, kiedy łańcuch znaków jest pusty za pomocą przełącznika ng. Poniższy kod nie działa dla mnie.ng-switch na pustym ciągu

<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> 

Każda pomoc w tym zakresie byłaby doceniana.

+0

Możliwe duplikat [angularjs ng przełącznika --- badania, jeśli wartość jest pusta lub nieokreślone] (http://stackoverflow.com/questions/25453689/angular-js-ng-switch-testing- wartość-wartość-jest-pusta-lub-niezdefiniowana) –

Odpowiedz

19
<div ng-switch on="foo || 'null'"> 
    <span ng-switch-when="null">This string is empty</span> 
    <span ng-switch-default>This string is not empty</span> 
</div> 
+0

to jest bardziej przydatne – manoj

2

Definite dupe of Angular.js ng-switch ---testing if value is empty or undefined.

Przyjęta odpowiedź też nie jest idealna. W mojej, tak pokornej opinii, najbardziej eleganckie i przejrzyste rozwiązanie ma najmniejsze poparcie.

<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>