ngResource(mongolabResource)保存后更新表

时间:2013-05-27 04:48:11

标签: mongodb angularjs

JS framework的新手,但有Java,JQuery等背景。 (这似乎对Angular没有帮助。)

我正在使用PKozlowski的优秀adapter用于Angular,并且在保存行时不知道如何更新UI(普通的vanilla表)。

从概念上讲,该表列出了被禁用的学生,在保存新名称和电子邮件后,它应该更新已经显示的表格。

这是在app.js:

app.controller('StudentCtrl', function($scope, Student){

      $scope.students = Student.query();

      $scope.addStudent = function() {
            var name = $scope.newName;
            var email = $scope.newEmail;

            var newStudent = Student.save({name: name, email: email});
            $scope.newName = '';
            $scope.email = '';
      }
});

这是我的HTML:

<body>
<div id="mainContainer" ng-controller="StudentCtrl">
    <input type="search" id="studentSearchbox" ng-model="studentSearch"></input>
    <div id="studentsList">
        <table>
            <thead>
                <tr>
                    <th>Student Name</th>
                    <th>Student Email</th>
                    <th>Student ID</th>
                </tr>
            </thead>
            <tbody>
                    <tr ng-repeat="student in students | filter:studentSearch" ng-class-even="'even'" >
                        <td>{{student.name}}</td>
                        <td>{{student.email}}</td>
                        <td>{{student._id.$oid}}
                    </tr>
            </tbody>
        </table>
        <hr />
        <div id="addStudentDiv">
            <h2>Add Student</h2>
            <label ng-model="newStudent">{{newStudent.name}}</label>
            <br />
            <input type="text" placeholder="Firstname-Lastname" ng-model="newName">Student Name</input>
            <input type="email" placeholder="Email" ng-model="newEmail"></input>
            <button type="button" ng-click="addStudent()">Submit</button>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

不确定这是否是“有角度”的方式,但我的解决方案是将此

$scope.students.push(newStudent);

var newStudent = Student.save({name: name, email: email});