2017-06-09 12 views
-1

私はそれをクリックして展開/縮小したいdivがあります。私はdiv要素を作成しました:シンプルなIf-thenステートメントが動作しない

<div ng-click="disclaimer();" style="height:100px;width:100px;overflow:{{expand}}">Sample Text</div> 

ユーザーがdiv要素をクリックすると、それは単に$を切り替えますscope.expandデフォルト(またはそれが必要atlteast)に隠されてから

$scope.disclaimer=function(){ 
    if($scope.expand="hidden"){ 
    $scope.expand="default"; 
    } 
    else if($scope.expand="default"){ 
     $scope.expand="hidden"; 
    } 
} 

は今、それはDIVを拡大します(したがって、$ scope.expandは非表示からデフォルトに変更されますが、divを再度クリックすると縮小しません)。何か案は?この

$scope.disclaimer=function(){ 
    if($scope.expand=="hidden"){ 
    $scope.expand="default"; 
    } 
    else if($scope.expand=="default"){ 
     $scope.expand="hidden"; 
    } 
} 

に助けてくれてありがとう

+7

'=' ''、割り当てのためのものです== 'と' ===を実行し、参照してください。比較のためです。 – Pointy

+0

つまり、最初の 'if'は常にtrueです。なぜならあなたは常に比較する(==)のではなく、(=)を割り当てるからです。したがって、拡大するが崩壊する理由はない。 – hack3rfx

答えて

1
$scope.disclaimer=function(){ 
    if($scope.expand="hidden"){ 
    $scope.expand="default"; 
    } 
    else if($scope.expand="default"){ 
     $scope.expand="hidden"; 
    } 
} 

を参照してくださいhere

+0

素晴らしい... ....ありがとうございました。 –

0

を忘れないでください:angularjsに、我々は、ビュー側あまりに代わりに、コントローラの機能を設定したりすることができます...

例えば、あなたの質問では、ビューを処理するためにコントローラ内の関数を使う必要はありません。 ng-styleまたはng-class

として、デフォルトのディレクティブの多くはサンプル

var app = angular.module("app", []);
.my-class { 
 
    height:100px; 
 
    width:400px; 
 
    border: solid 1px #ccc; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="app"> 
 
    <button ng-click="expand = !expand" >click me</button> 
 
    
 
    <b>expand is {{expand}} then overflow is {{expand ? 'auto':'hidden'}}</b> 
 
    <br> 
 
    <br> 
 
    <div class="my-class" 
 
    ng-style="{'overflow': expand ? 'auto':'hidden'}"> 
 
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. 
 
    </div> 
 
    
 
    
 
</div>

関連する問題