ng-model在ng-repeat下的select标签中不起作用

时间:2016-06-16 11:57:02

标签: angularjs html-select angular-ngmodel angularjs-select

这是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);
}

enter image description here 我正在使用物质css。当我选择选项时,ng-change不会触发,也不会将数据绑定到ng-model。

任何人都可以帮助我在哪里犯错误吗?

This is plnkr link

2 个答案:

答案 0 :(得分:1)

问题在于您包含脚本文件的顺序。

检查羽毛球。

https://plnkr.co/edit/eFL48s3UyhwdTFzwTR05

ng-model正确绑定,ng-change也被正确解雇。

library(clipr)
clipr::write_clip(mtcars)

答案 1 :(得分:0)

首先,不需要选择ng-ng。如果下拉列表具有该值,则ng-model将自动检测从下拉列表中选择的内容。

所以将您的选择更改为

<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>

根据你的plnkr,还包括jquery,你没有包含那个

<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>

这是工作的plnkr

https://plnkr.co/edit/p7t7KcNmChZlT5Jg0pr2?p=preview