2016-12-14 65 views
2

我在我的ng-options表達式中使用過濾器將所有選項轉換爲大寫,但現在當我想要獲取值時用戶選擇的,我得到一個未定義如何在ng選項表達式中使用過濾器後在AngularJS中獲得html選擇的值

這裏是我的選擇

<select name="selectThings" class="form-control" 
            id="selectThings" 
            ng-model="selectThings" 

            ng-options="thing.name as (thing.name | uppercase) for thing in thinglist" 
            ng-change="submitSelectedValThing()" 
            required> 
</select> 

,這裏是如何我試圖讓用戶選擇

$scope.selectThings.name; 

的價值,但我得到一個undefin編輯錯誤在該行,該代碼用於正常工作之前,我添加了過濾器

這裏是我的全部控制器

angular.module('MyApp') 
    .controller('ThingCtrl', ['$scope', 
     function ($scope) { 


      $scope.submitSelectedValThing = function() { 
      $scope.$watch('thing.Name', function (newVal, oldVal) { 
       if (!$scope.thing.Name || newVal === oldVal) return; 
        // now do stuff here with $scope.selectedThing 
        console.log('the value is ' + $scope.selectThings.name); 
      }); 
      console.log($scope.selectThings.name); 

      }; 




     }]); 
+1

「中使用的代碼,以做工精細我加入之前過濾器」可以按代碼的工作方式發佈代碼? – Claies

回答

0

你真的想在這裏是什麼

<select ng-model="selectedThing" ng-options="formattedThingName as (thing.name | uppercase) for thing in thinglist"></select> 

選定值現在可以通過$ scope.selectedThing訪問

我不太確定你的代碼的結果,但我的猜測是它將大寫過濾器的值重新分配給第e thing.name。

+0

我想獲得用戶選擇的值,所以我可以在我的控制器中使用該值。我嘗試在我的控制器中放入這個「$ scope.selectThings.name;」但我得到未定義,有沒有辦法獲得一個選擇的價值,如果你使用像大寫的過濾器? –

+0

爲了保持簡短,我刪除了ng模型,但病毒更新包含它。你應該得到預期的效果。 – Belfordz

+0

我得到了相同的未定義錯誤,我編輯了我的問題,我看到 中的類型錯誤「對於thingsin事物列表」我固定它爲「thinglist中的東西」,但我得到相同的錯誤 –

0

您可以將手錶添加到下拉勢必值,然後在表達式求值,像這樣比較值:

 // 
     // Handle dropdown on select 
     $scope.$watch('selectThings.name', function (newVal, oldVal) { 
      if (!$scope.selectThings.name || newVal === oldVal) return; 
       // now do stuff here with $scope.selectThings.name 
      ); 
     }); 
+0

它沒有工作我更新了我的問題與您的代碼 –

+0

我更新了我的代碼示例,但我不能保證它不會看到如何定義selectThings.name和您的代碼的其餘部分。請製作一個JSFiddle或提供一個工作代碼示例。 – nocarrier

相關問題