2012-09-15 20 views
12

Angular kullanıyorum ve bir dize ng-switch kullanarak ne zaman boş olduğunu kontrol etmek istiyorum. Aşağıdaki kod benim için çalışmıyor gibi görünüyor.Boş dize ng geçişi

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

Bu konuda herhangi bir yardım mutluluk duyacaktır.

+0

Tekrarlama ([angularjs ng şalter --- test değeri boş veya tanımlanmamış ise] http://stackoverflow.com/questions/25453689/angular-js-ng-switch-testing- if-value-boş-veya-undefined) –

cevap

11

Sen denetleyici boş dize eklenmesi gerekmektedir: API bakınız Ek Referans için

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

: http://jsfiddle.net/upPgU/

+1

Temel kullanım durumu, bir geçici çözüm gerektirir :(. – Den

2

Angular.js ng-switch ---testing if value is empty or undefined ürününün kesin kopyası.

Kabul edilen cevap da burada ideal değil. Benim çok alçakgönüllü düşüncemde, en zarif ve açık çözüm, oradaki en az yükseltiye sahiptir.

<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> 
arasında