2016-11-15 22 views
0

我有一塊手錶region,但當單擊單選按鈕時它不會觸發。角度無線電按鈕不會燒上手錶

我做錯了什麼或者是因爲單選按鈕只檢測到一次角度變化?無論如何,這不使用表單?

<div class="filter-column"> 
    <div class="filter-title">Region</div> 
    <div class="bottom-line"></div> 
    <div class="overflow-container"> 
     <div ng-repeat="choice in regions| orderBy: 'description'"> 
     <input type="radio" 
      value="{{choice.name}}" 
      name="regionRadio" 
      ng-model="region"> 
     {{choice.description}} 
     </div> 
    </div> 
    </div> 

$scope.$watchCollection(watchRMDapisPopulate, 
    populateRMDfiltersView); 

    function watchRMDapisPopulate() { 
     return regionMarketDealerSrv.orgFilterData; 
     } 

     function populateRMDfiltersView(newValue, oldValue) { 
     if (newValue) { 
      $scope.regions = newValue.regions; 
     } 
     } 



     $scope.$watch('region', radioButtonRegion); 

     function radioButtonRegion(regionName) { 
     console.log(regionName) 
     if (regionName === 'All') { 
      regionMarketDealerSrv.populateRegions(); 
     } else { 
      regionMarketDealerSrv.populateMarkets(regionName); 
     } 
     } 
+1

嘗試更改值= 「{{choice.name}}」 到NG-值=「{{選擇.name}}「 –

+0

」將其更改爲「ng值」,但仍然沒有成功。沒有'console.log(regionName)'輸出。 – dman

+1

如何使用功能作爲手錶表達? $ scope。$ watch(function(){return $ scope.region;},radioButtonRegion) –

回答

2

手錶是無法使用ng-model="region"時,因爲使用的是原始值正確地拿起變化。當您更改該值時,您將獲得新的參考。您可以使用一個對象的屬性爲你的模型和$watch是:

var app = angular.module('app', []) 
 

 
app.controller('MyController', function($scope) { 
 
    $scope.regions = [{ 
 
    name: 'a', 
 
    description: 'region A' 
 
    }, { 
 
    name: 'b', 
 
    description: 'region B' 
 
    }]; 
 

 
    $scope.selected = { 
 
    region: 'a' 
 
    }; 
 

 
    $scope.$watch('selected.region', radioButtonRegion); 
 

 
    function radioButtonRegion(newVal, oldVal) { 
 
    if (newVal !== oldVal) { 
 
     console.log(newVal, oldVal) 
 
    } 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="MyController"> 
 
    <div>Region</div> 
 
    <div ng-repeat="choice in regions| orderBy: 'description'"> 
 
    <input type="radio" value="{{choice.name}}" name="regionRadio" ng-model="selected.region">{{choice.description}} 
 
    </div> 
 
</div>

+0

啊....我正在使用,但我沒有辦法設置一個默認的選中值。關於如何設置默認選中值的任何想法,因爲我不能使用ng-checked? – dman

+1

您可以將初始值設置爲'$ scope.selected'對象。 – DTing