使用ng-model将值绑定到ng-init

时间:2017-06-27 07:48:50

标签: angularjs

我有一个toString()字段和一个'输入'字段,两个字段都从数据库中获取数据。
所选选项将默认根据db值进行检查。当我更改下拉选项时,select字段将自动填充。
现在我的问题是,当我更改下拉列表时,balance字段值是自动填充的。但默认情况下,平衡字段值不会自动填充所选选项。
这里有什么问题?

Html:

balance

脚本:

<form ng-app="myApp" ng-controller="myCtrl" ng-repeat="data in leaveDetails">

        <div>
            <label>Leave Type</label>
            <select type="text" ng-model="selectedName" ng-init="selectedName=data.LeavetypeId" ng-options="data.id as data.Name for data in leaveTypes" ng-change="leaveBalance(selectedName)"></select>
        </div>
        <div>
            <label>Availabe Leaves</label>
            <input type="text"  ng-init="balanceCount=data.Balance" ng-model="balanceCount">
        </div>

 </form>

1 个答案:

答案 0 :(得分:1)

以下是实现您要执行的操作的示例代码!希望它会有所帮助!!

&#13;
&#13;
<!doctype html>
<html lang="en" data-ng-app="test">
<head>
    <meta charset="utf-8">
    <title>Test</title>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
    <script type="text/javascript">

        angular.module('test',[])
                .controller('Main',function($scope)
                {
                    $scope.myModel = {};
                    $scope.options = [{opt:'1'},{opt:'2'},{opt:'3'}];
                });
    </script>
</head>
<body ng-controller="Main">
{{'Select'}}
<select ng-model="myModel.options" ng-options="data.opt for data in options" ng-init="myModel.options=options[0]"></select>
{{myModel.options}}
</body>
</html>
&#13;
&#13;
&#13;