2015-06-25 16 views
0

上週我做了一個函數,它從一對按鈕(一個是true,一個是false)中取出一個true/false值並將其放入$ scope變量。

它工作完美。

$scope.trueOrFalse = function(truefalse) { 
 
    if (truefalse == false){ 
 
     $scope.value = false; 
 
     document.getElementById("makeFalse").className = "button active"; 
 
     document.getElementById("makeTrue").className = "button"; 
 
     console.log($scope.isBusiness); 
 
    } else if(truefalse == true){ 
 
     $scope.value = true; 
 
     document.getElementById("makeTrue").className = "button active"; 
 
     document.getElementById("makeFalse").className = "button"; 
 
     console.log($scope.value); 
 
    } 
 
    }

但現在,我已經基本上同樣的事情在另一個網頁,其唯一的工作一次,在第一次點擊,其中,如果選擇的值是假的,我得到一個console.log它是錯誤的,那麼如果再次點擊,就不會發生任何改變。

但如果選擇的值是true,那麼下一次點擊嘗試,我開始得到:

類型錯誤:FN是不是一個函數

at $parseFunctionCall (ionic.bundle.js:21045); 
at ionic.bundle.js:53458; 
at Scope.$get.Scope.$eval (ionic.bundle.js:23100); 
at Scope.$get.Scope.$apply (ionic.bundle.js:23199); 
at HTMLAnchorElement.<anonymous> (ionic.bundle.js:53457); 
at HTMLAnchorElement.eventHandler (ionic.bundle.js:11713); 
at triggerMouseEvent (ionic.bundle.js:2863); 
at tapClick (ionic.bundle.js:2852); 
at HTMLDocument.tapMouseUp (ionic.bundle.js:2925); 

這是怎麼回事?

這裏的HTML

<ion-view view-title="Biz Post/Uploading"> 
 
    <ion-content> 
 
      
 

 
    <!-- Begin Second Set of Tabs --> 
 
    <div class="button-bar"> 
 
    <a class="button" id="item" data-ng-click="isDeal(false)">Item</a> 
 
    <a class="button" id="deal" data-ng-click="isDeal(true)">Deal</a> 
 
    </div> 
 
    <!-- End Second Set of Tabs --> 
 

 
    <!-- Section --> 
 
    <div class="list-inset"> 
 
    <center><img src="http://placehold.it/350x150"></center> 
 
    </div> 
 

 

 
    <div class="list"> 
 
    <div class="item item-divider"> 
 
     Add thing 
 
     </div> 
 
     <label class="item item-input item-stacked-label"> 
 
     <span class="input-label">thing</span> 
 
     <input type="text" placeholder="add location" ng-model="thing.thing2"> 
 
     </label> 
 
     <label class="item item-input item-stacked-label"> 
 
     <span class="input-label">thing</span> 
 
     <input type="text" placeholder="add price" ng-model="thing.thing3"> 
 
     </label> 
 
     <label class="item item-input item-stacked-label"> 
 
     <span class="input-label">thing</span> 
 
     <input type="text" placeholder="add item description" ng-model="item.thing"> 
 
     </label> 
 
     <label class="item item-input"> 
 
    <span class="input-label">Date</span> 
 
    <input type="date"> 
 
</label> 
 
    <label class="item item-input item-stacked-label"> 
 
     <span class="input-label">thing</span> 
 
     <input type="text" placeholder="set limit" ng-model="thing.thing4"> 
 
     </label> 
 

 
      <button class="button button-full button-balanced" data-ng-click="pushData()"> 
 
      push data 
 
      </button> 
 

 
    
 
     </div> 
 
    </ion-content> 
 
</ion-view>

+0

請還顯示代碼HTML與兩個按鈕 – manzapanza

+0

是的控制器正在加載新頁面?也許範圍不會在第一次點擊之後更新。 – isherwood

+0

是的,控制器被加載,它打印出第一個console.log的真或假值 – Femtosecond

回答

0

隨着納克級的代碼是這樣的: http://plnkr.co/edit/6sOzUmAcIzVjLEOL9Jva?p=preview

<div ng-controller="MainCtrl"> 
     <button id="makeTrue" ng-class="{button :true, active: truefalse}" ng-click="truefalse = !truefalse">Make true</button> 
     <button id="makeFalse" ng-class="{button :true, active: !truefalse} " ng-click="truefalse = !truefalse">Make false</button> 
    </div> 

在控制器:$scope.truefalse = true;。如果truefalse爲true,那麼makeTrue具有活動類(這似乎有點不合適,但在代碼中相同),否則makeFalse處於活動狀態。

這樣你就不會在你的控制器中玩弄DOM(這應該避免)。並且你的$範圍不那麼混亂。

對於NG單擊您可以定義在$範圍的功能,並調用它,而不是改變表達式truefalse的(但是這取決於你)