2016-01-24 57 views
0

我已經預定義的圖像,我想做一個微調,用戶可以選擇這些項目;但我也想爲自定義圖像選擇一個文件按鈕。angularjs連接下拉選擇文件

<div> 
    <select class="form-control" ng-model="inputQ.image"> 
    <!-- show if there is no image --> 
    <option disabled value="{{ undefined }}" 
      ng-hide="inputQ.image && !isImageFromSpinner(inputQ.image)"> 
     Nichts ausgewählt 
    </option> 
    <!-- show if there is a custom image --> 
    <option disabled 
      ng-show="inputQ.image && !isImageFromSpinner(inputQ.image)" 
      value="{{ isImageFromSpinner(inputQ.image) ? undefined : inputQ.image }}"> 
     Benutzerdefiniertes Bild ausgewählt 
    </option> 
    <option ng-repeat="res in exerciseRessources.images" 
      value="{{res.blob}}" ng-disabled="res.disabled"> 
     {{res.name}} 
    </option> 
    </select> 
    <br> 
</div> 
<div file-chooser ng-model="inputQ.image" accept="image/*"> 
    <img class="input" ng-src="{{inputQ.image}}" alt="Beispiel" 
     ng-show="inputQ.image"> 
    <br ng-show="inputQ.image"> 
    <button class="btn btn-default" file-select>Bild auswählen... 
    </button> 
    <button class="btn btn-default" file-unselect> 
    <span class="glyphicon glyphicon-trash"></span> 
    </button> 
</div> 

我想在第二個選項中選擇處理自定義圖像,第一個是init。

但函數isImageFromSpinner返回true或false。

$scope.isImageFromSpinner = function (model) { 
    $scope.exerciseRessources.images.forEach(function (img) { 
     if (img.blob && img.blob === model) { 
      $log.warn(model, true); 
      return true; 
     } 
    }); 
    $log.warn(model, false); 
    return false; 
}; 

如果元素在微調器中,則它應該爲true,否則爲false。不是true,false,true,...
所以這也難怪,選擇選項不能正常工作。

有人知道,爲什麼我得到它或者真或假?

+0

您不能在'

+0

下工作真的非常感謝你輸入 – rala

回答

1
$scope.isImageFromSpinner = function (model) { 
    returnVal = false; 
    $scope.exerciseRessources.images.forEach(function (img) { 
     if (img.blob && img.blob === model) { 
      returnVal = true; 
      $log.warn(model, returnVal); 
      break; 
     } 
    }); 
    return returnVal; 
}; 
+1

;太簡單?你能解釋爲什麼嗎? ^^ – rala

+1

您的原始代碼返回兩次,第二個始終爲false。你真正想要做的是找到是否有任何匹配,並立即從循環中突然返回值。因此,我只是略微修改了你的代碼來做到這一點。 :-) –