SORU
13 Mart 2013, ÇARŞAMBA


AngularJS toggle kullanarak sınıf-sınıf ng

Bir öğe ng-class Kullanma sınıfı geçiş yapmak için çalışıyorum

<button class="btn">
  <i ng-class="{(isAutoScroll()) ? 'icon-autoscroll' : 'icon-autoscroll-disabled'}"></i>
</button>

() isAutoScroll:

$scope.isAutoScroll = function()
{
    $scope.autoScroll = ($scope.autoScroll) ? false : true;
    return $scope.autoScroll;
}

Eğer $scope.autoScroll doğru ise temelde, sınıf ng icon-autoscroll olmak istiyorum ve eğer yanlış, o icon-autoscroll-disabled olmak istiyorum

Ben şimdi ne var ve konsolda bu hata üretiyor işe yaramıyor

Error: Lexer Error: Unexpected next character at columns 18-18 [?] in expression [{(isAutoScroll()) ? 'icon-autoscroll' : 'icon-autoscroll-disabled'}].

Nasıl doğru yapacağım?

EDİT

çözüm 1: (Eski)

<button class="btn" ng-click="autoScroll = !autoScroll">
  <i ng-class="{'icon-autoscroll': autoScroll, 'icon-autoscroll-disabled': !autoScroll}"></i>
</button>

2 DÜZENLEYİN

çözüm 2:

Çözüm 3 Stewie tarafından sağlanan bir kullanılmalıdır çözüm bilgilendirmek istedim. Üçlü operatör (ve bana en kolay okumak için) konusunda en standarttır. Çözüm olurdu

<button class="btn" ng-click="autoScroll = !autoScroll">
  <i ng-class="autoScroll ? 'icon-autoscroll' : 'icon-autoscroll-disabled'"></i>
</button>

çeviren:

if (autoScroll == true) ? //'icon-autoscroll' : //kullanım sınıfı başka 'icon-autoscroll-disabled' kullanın

CEVAP
13 Mart 2013, ÇARŞAMBA


Nasıl ng-sınıfı: koşullu kullanmak için

Çözüm 1:

<i ng-class="{'icon-autoscroll': autoScroll, 'icon-autoscroll-disabled': !autoScroll}"></i>

Çözüm 2:

<i ng-class="{true: 'icon-autoscroll', false: 'icon-autoscroll-disabled'}[autoScroll]"></i>

Çözüm 3 (açısal 1 v..1.4 operatör üçlü) için destek tanıttı:

<i ng-class="autoScroll ? 'icon-autoscroll' : 'icon-autoscroll-disabled'"></i>

Plunker

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Paste Magazine

    Paste Magazi

    28 AĞUSTOS 2008
  • Rickymon Tero

    Rickymon Ter

    1 Ocak 2007
  • The Warp Zone

    The Warp Zon

    24 AĞUSTOS 2007