如何在ng-model上使用数组

时间:2016-04-11 21:14:18

标签: angularjs

这是我的代码http://plnkr.co/edit/oxtojjEPwkKng9iKkc14?p=preview

我想在一个数组中保存运动和标点符号的对象,如果有一个或多个运动选择将它保存在数组中,如下所示:

likes[
{sport: 'futball', points: 1}, {sport: 'tennis', points: 1}
]

谢谢!

1 个答案:

答案 0 :(得分:0)

你必须多练习一下你的英语(你可以在西班牙语的评论部分问我,如果你正在捣蛋)

我认为你要做的就是让一个选择能够选择这项运动和得分,

HTML:

<body ng-app="myapp">
    <div class="main-container" ng-controller="main" ng-init="darr.dept=dept2">
        <div class="client-area">
            <label fo.table-container tabler="txt">Score</label>
            <input type="text" id="name-txt" placeholder="Assign the score" ng-model="name">
            <br />
            Sport
            <select ng-model="dept" ng-options="deptOpt.value as deptOpt.name for deptOpt in deptList"></select>
            <br />
            <button ng-click="add()">Add Item</button>
            <table id="tab">
                <thead>
                    <tr id="trow">
                        <th>Score</th>
                        <th>Sport</th>
                        <th>Options</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="ar in arr">
                        <td>{{ar.name}}</td>
                        <td>{{ar.dept}}</td>
                        <td>
                            <button ng-click="del($index)">Delete</button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</body>

JS:

var app = angular.module("myapp", []);
app.controller('main', function ($scope) {
    $scope.arr = [];
    $scope.deptList = [{
        name: 'Football',
        value: 'football'
    }, {
        name: 'Tennis',
        value: 'Tennis'
    }, {
        name: 'Baseball',
        value: 'baseball'
    }];
    $scope.dept = "football";
    $scope.name = "";
    $scope.add = function () {
        this.arr.push({
            name: this.name,
            dept: this.dept
        });
    };
    $scope.del = function (ind) {
        this.arr = this.arr.splice(ind, 1);
    };
    $scope.editStudent = function(student) {
      console.log(student);  
    };
});

https://jsfiddle.net/azweig/v5kbsudy/1