删除选项后,AngularJS不会更新选择

时间:2014-07-27 10:59:06

标签: angularjs select ng-options

Angular在删除后不会更新选择框的选项。我必须刷新视图才能看到更新的选项。谢谢。

我的观点

form(ng-controller="FormRemoveStudentController", name="formAddStudentForm", novalidate)
    div.combine
        label.default.top(for="student") Select a student
        select.default(ng-model="student.id", ng-options="s.id as s.name for s in students")
            option(value="") I would like to remove...
    div.combine
        button.default.blue(ng-click="removeStudent()") Submit

我的控制器

app.controller('FormRemoveStudentController', ['$scope', '$http', function ($scope, $http) {
    $scope.student = {};
    $scope.students = [];

    var setStudents = function (data) {
        $scope.students = data.students || [];
        console.log("Students has updated.")
    };

    var errorStudents = function () {
        $scope.students = [];
        console.error("DATABASE ERROR");
    };

    $http.get(url).success(setStudents).error(errorStudents);


    $scope.removeStudent = function () {
        var finalURL = url + $scope.student.id;

        // $http.delete(final); this is supposed to work if the server was fine.
        conn.delete(finalURL); // Delete the option

        $scope.student = {};
        $scope.formAddStudentForm.$setPristine();

        $http.get(url).success(setStudents).error(errorStudents); // Reload the options.
    };
}])

1 个答案:

答案 0 :(得分:0)

在选择中将ng-model设置为student,并将其默认为null而不是空对象。 在删除功能中,将student重置为空

还要确保正确更新ng选项