ng-model 在 ng-repeat 下的 select 标签中不起作用
ng-model not working in select tag under ng-repeat
这是 html 页
<form name="createPromoterFormName">
<div class="col s12" ng-repeat="customer in model.customers" ng-if="model.selectedCustomerSubtab == $index">
<div class="row inputcontainer">
<div class="input-field col s4">
<input placeholder="First Name" type="text" ng-model="customer.customer_name" class="validate" ng-maxlength="50" ng-required="true" />
<label class="active labelName">Customer Name </label>
</div>
<div class="input-field col s4">
<input type="text" class="validate" ng-maxlength="50" ng-required="true" ng-model="customer.avg_monthly_sales" />
<label class="active labelName">Average Monthly Sales </label>
</div>
<div class="input-field col s4">
<select ng-model="customer.avg_payment_cycle_days" ng-options="i.value as i.value for i in model.avgPaymentCycleDays" ng-change="changeSelectedItem()">
<option value="">Select Average Payment Cycle Days</option>
</select>
<label class="labelName">Avg Payment Cycle Days [[customer.avg_payment_cycle_days]]</label>
</div>
</div>
<div class="row inputcontainer">
<div class="input-field col s4">
<input type="text" class="validate" ng-maxlength="50" ng-model="customer.avg_paymmonths_in_business_with_customerent_cycle_days" />
<label class="active labelName">Months in business with customer</label>
</div>
</div>
</div>
</form>
Controller.js
$scope.model = {
customers: [{
customer_name: "",
avg_monthly_sales: "",
avg_payment_cycle_days: "",
months_in_business_with_customer: ""
}, {
customer_name: "",
avg_monthly_sales: "",
avg_payment_cycle_days: "",
months_in_business_with_customer: ""
}, {
customer_name: "",
avg_monthly_sales: "",
avg_payment_cycle_days: "",
months_in_business_with_customer: ""
}],
avgPaymentCycleDays : [{
value: '15',
id: '1'
}, {
value: '30',
id: '2'
}, {
value: '45',
id: '3'
}, {
value: '60',
id: '4'
}, {
value: '75',
id: '5'
}, {
value: '90',
id: '6'
}],
selectedPromoterSubtab: 0,
selectedCustomerSubtab: 0,
}
$scope.changeSelectedItem = function() {
console.log($scope.model.customers);
}
我正在使用 material css。当我选择选项时,ng-change 没有触发,也没有将数据绑定到 ng-model。
谁能帮我看看我哪里出错了?
首先不需要 ng-selected。 ng-model 将自动检测必须从下拉列表中 selected 的内容,如果下拉列表具有该值。
所以把你的select改成
<select ng-model="customer.avg_payment_cycle_days" ng-options="i.value as i.value for i in model.avgPaymentCycleDays" ng-change="changeSelectedItem()">
<option value="">Select Average Payment Cycle Days</option>
</select>
还包括 jquery 根据您的 plnkr,您没有包括
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
这是工作计划
问题在于您包含脚本文件的顺序。
检查 plunker。
https://plnkr.co/edit/eFL48s3UyhwdTFzwTR05
ng-model 正确绑定并且 ng-change 也正确触发。
<script data-require="jquery" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
这是 html 页
<form name="createPromoterFormName">
<div class="col s12" ng-repeat="customer in model.customers" ng-if="model.selectedCustomerSubtab == $index">
<div class="row inputcontainer">
<div class="input-field col s4">
<input placeholder="First Name" type="text" ng-model="customer.customer_name" class="validate" ng-maxlength="50" ng-required="true" />
<label class="active labelName">Customer Name </label>
</div>
<div class="input-field col s4">
<input type="text" class="validate" ng-maxlength="50" ng-required="true" ng-model="customer.avg_monthly_sales" />
<label class="active labelName">Average Monthly Sales </label>
</div>
<div class="input-field col s4">
<select ng-model="customer.avg_payment_cycle_days" ng-options="i.value as i.value for i in model.avgPaymentCycleDays" ng-change="changeSelectedItem()">
<option value="">Select Average Payment Cycle Days</option>
</select>
<label class="labelName">Avg Payment Cycle Days [[customer.avg_payment_cycle_days]]</label>
</div>
</div>
<div class="row inputcontainer">
<div class="input-field col s4">
<input type="text" class="validate" ng-maxlength="50" ng-model="customer.avg_paymmonths_in_business_with_customerent_cycle_days" />
<label class="active labelName">Months in business with customer</label>
</div>
</div>
</div>
</form>
Controller.js
$scope.model = {
customers: [{
customer_name: "",
avg_monthly_sales: "",
avg_payment_cycle_days: "",
months_in_business_with_customer: ""
}, {
customer_name: "",
avg_monthly_sales: "",
avg_payment_cycle_days: "",
months_in_business_with_customer: ""
}, {
customer_name: "",
avg_monthly_sales: "",
avg_payment_cycle_days: "",
months_in_business_with_customer: ""
}],
avgPaymentCycleDays : [{
value: '15',
id: '1'
}, {
value: '30',
id: '2'
}, {
value: '45',
id: '3'
}, {
value: '60',
id: '4'
}, {
value: '75',
id: '5'
}, {
value: '90',
id: '6'
}],
selectedPromoterSubtab: 0,
selectedCustomerSubtab: 0,
}
$scope.changeSelectedItem = function() {
console.log($scope.model.customers);
}
谁能帮我看看我哪里出错了?
首先不需要 ng-selected。 ng-model 将自动检测必须从下拉列表中 selected 的内容,如果下拉列表具有该值。
所以把你的select改成
<select ng-model="customer.avg_payment_cycle_days" ng-options="i.value as i.value for i in model.avgPaymentCycleDays" ng-change="changeSelectedItem()">
<option value="">Select Average Payment Cycle Days</option>
</select>
还包括 jquery 根据您的 plnkr,您没有包括
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="app.js"></script>
这是工作计划
问题在于您包含脚本文件的顺序。
检查 plunker。
https://plnkr.co/edit/eFL48s3UyhwdTFzwTR05
ng-model 正确绑定并且 ng-change 也正确触发。
<script data-require="jquery" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js"></script>
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>