2016-11-17 73 views
0

我在這裏有2個下拉菜單。我的問題是,我只想獲取省下拉列表中的元素值,並以ng爲單位傳遞變量 - 如果在城市下拉列表中。例如,所選省份的值是3,那麼ng-if是值=== province.city.province_id。請幫忙。謝謝。獲取元素值中的值並在ng中使用它 - if

$http.get(API_URL + "/register").then(function(response) { 
 
    $scope.countries = response.data['countries']; 
 
    $scope.provinces = response.data['provinces']; 
 

 
    //alert(response.data['provinces'].toSource()); 
 
    /*$('select[id=selProvince]').change(function() { 
 
\t \t \t \t \t var tempProv = $(this).val(); 
 
\t \t \t \t \t \t 
 
\t \t \t \t });*/ 
 

 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="form-group form-group-default"> 
 
    <label class="">Province</label> 
 
    <select id="selProvince" class="full-width" data-placeholder="Select Province" data-init-plugin="select2" tabindex="-1" title=""> 
 

 
    <option ng-repeat="province in provinces" value="{{province.id}}">{{province.name}}</option> 
 

 
    </select> 
 
</div> 
 

 
<div class="form-group form-group-default"> 
 
    <label class="">City</label> 
 
    <select id="selCity" class="full-width" data-placeholder="Select City" data-init-plugin="select2" tabindex="-1" title=""> 
 

 
    <option ng-repeat="province in provinces" ng-if="value === province.city.province_id" value="{{province.city.id}}">{{province.city.name}}</option> 
 

 
    </select> 
 
</div>

+0

你的js代碼在哪裏? – Sreekanth

+0

你試過'$ scope.value'嗎? – Prometheus

+0

我在我的js裏面有這個函數,但是我不能把函數裏面的函數傳給我的js,在我的html – kwell

回答

0

您需要使用NG-模型。

<select id="selProvince" class="full-width" data-placeholder="Select Province" data-init-plugin="select2" tabindex="-1" title="" ng-model="provinceSelected"> 

<option ng-repeat="province in provinces" value="{{province.id}}">{{province.name}}</option> 

然後在第二個選擇框

<option ng-repeat="province in provinces" ng-if="provinceSelected === province.city.province_id" value="{{province.city.id}}">{{province.city.name}}</option> 

0

使用NG-模型

<div class="form-group form-group-default"> 
    <label class="">Province</label> 
    <select id="selProvince" class="full-width" ng-model="selProvince" data-placeholder="Select Province" data-init-plugin="select2" tabindex="-1" title=""> 
     <option ng-repeat="province in provinces" value="{{province.id}}">{{province.name}}</option> 
    </select> 
</div> 

<div class="form-group form-group-default"> 
    <label class="">City</label> 
    <select id="selCity" class="full-width" data-placeholder="Select City" data-init-plugin="select2" tabindex="-1" title=""> 
     <option ng-repeat="province in provinces" ng-if="selProvince === province.city.province_id" value="{{province.city.id}}">{{province.city.name}}</option> 

    </select> 
</div> 
+0

仍然不工作:( – kwell

+0

你有沒有更新第一個選擇框中的ng模型和ng-if在第二個選擇框中 –

+0

我想你錯過了'ng-if =「selProvince === province.city.province_id」 '在第二個選擇框 –

0

它現在的工作感謝所有誰回答:)你們搖滾!這就是我所做的

$http.get(API_URL + "/register").then(function(response) { 
 
    $scope.countries = response.data['countries']; 
 
    $scope.provinces = response.data['provinces']; 
 
    $scope.cities = response.data['cities']; 
 
    //alert(response.data['provinces'].toSource()); 
 
    /*$('select[id=selProvince]').change(function() { 
 
\t \t \t \t \t var tempProv = $(this).val(); 
 
\t \t \t \t \t \t 
 
\t \t \t \t });*/ 
 

 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="form-group form-group-default"> 
 
    <label class="">Province</label> 
 

 

 
    <select id="selProvince" class="full-width" ng-model="selProvince" data-placeholder="Select Province" data-init-plugin="select2" tabindex="-1" title=""> 
 

 
    <option ng-repeat="province in provinces" value="{{province.id}}">{{province.name}}</option> 
 

 
    </select> 
 
</div> 
 

 
<div class="form-group form-group-default"> 
 
    <label class="">City</label> 
 
    <select id="selCity" class="full-width" data-placeholder="Select City" data-init-plugin="select2" tabindex="-1" title=""> 
 

 
    <option ng-repeat="city in cities" ng-if="selProvince == city.province_id" value="{{city.id}}">{{city.name}}</option> 
 

 
    </select> 
 
</div>

然後我laravel控制器:

$country = Country::All(); 
$province = Province::All(); 
$city = City::All(); 
return array('countries'=>$country , 'provinces'=>$province , 'cities'=>$city); 

再次感謝你。