Javascript 如何在关闭后重置所有HTML输入字段

Javascript 如何在关闭后重置所有HTML输入字段,javascript,html,angularjs,Javascript,Html,Angularjs,我将在文本字段中输入用户名和密码,但不会保存。不保存该数据,我将关闭弹出模式 当我在模式外单击时,它将关闭 如果我再次打开弹出窗口,以前的数据仍会出现。我需要清除这些数据,并希望将字段设置为空 请检查以下链接 angular.module('plunker',['ui.bootstrap']); var ModalDemoCtrl=函数($scope、$modal、$log){ $scope.user={ 用户:“名称”, 密码:null, 注:空 }; $scope.open=函数(){ $

我将在文本字段中输入用户名和密码,但不会保存。不保存该数据,我将关闭弹出模式

当我在模式外单击时,它将关闭

如果我再次打开弹出窗口,以前的数据仍会出现。我需要清除这些数据,并希望将字段设置为空

请检查以下链接

angular.module('plunker',['ui.bootstrap']);
var ModalDemoCtrl=函数($scope、$modal、$log){
$scope.user={
用户:“名称”,
密码:null,
注:空
};
$scope.open=函数(){
$modal.open({
templateUrl:'myModalContent.html',//加载模板
background:true,//设置background允许我们在模式窗口外单击关闭模式窗口
windowClass:'模态',//windowClass-要添加到模态窗口模板的其他CSS类
控制器:函数($scope、$modalInstance、$log、user){
$scope.user=用户;
$scope.submit=函数(){
$log.log('提交用户信息');//类似于控制台记录此语句
$log.log(用户);
$modalInstance.DISCLISE('cancel');//DISCLISE(REASURE)-一种可用于解除模态并传递原因的方法
}
$scope.cancel=函数(){
$modalInstance.disclose('cancel');
};
},
决心:{
用户:函数(){
返回$scope.user;
}
}
});//modal.open的结尾
};//作用域结束。打开函数
};

我是模态的!
用户名
密码
添加一些注释
取消
打开我!
从模式中选择:{{selected}

问题是,您实际上是在将模态中的
$scope.user
绑定到
ModalDemoCtrl
中的
$scope.user
。要解决此问题,在模态控制器中使用
用户之前,应先复制一份该用户的

$modal.open({
     ...
     controller: function ($scope, $modalInstance, $log, user) {
         $scope.user = angular.copy(user);
         ...
     }
});

请参阅文档。

有关如何清除表单,请参阅。您可以在模式关闭之前在submit函数中触发此操作

您可以通过ui引导承诺做到这一点:

$scope.modal.result.then(function(result) {
    console.log('client: resolved: ' + result);
  }, function(reason) {
    console.log('client: rejected: ' + reason);
});
我已经运行了你的代码,它运行得很好

angular.module('plunker', ['ui.bootstrap']);
    var ModalDemoCtrl = function ($scope, $modal, $log) {

    $scope.user = {
        user: 'name',
        password: null,
        notes: null
    };

    $scope.open = function () {

        var $theModal = $modal.open({
            templateUrl: 'myModalContent.html', // loads the template
            backdrop: true, // setting backdrop allows us to close the modal window on clicking outside the modal window
            windowClass: 'modal', // windowClass - additional CSS class(es) to be added to a modal window template
            controller: function ($scope, $modalInstance, $log, user) {
                $scope.user = user;
                $scope.submit = function () {
                    $log.log('Submiting user info.'); // kinda console logs this statement
                    $log.log(user); 
                    $modalInstance.dismiss('cancel'); // dismiss(reason) - a method that can be used to dismiss a modal, passing a reason
                }
                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel'); 
                };
            },
            resolve: {
                user: function () {
                    return $scope.user;
                }
            }
        });//end of modal.open

        $theModal.result.then(function(result) {
            console.log('client: resolved: ' + result);
        }, function(reason) {
            $scope.user = {}
            console.log('client: reject: ' + reason);
        })
    }; // end of scope.open function
};

打开modalI时,将值分配给空字符串。这里有一些下拉列表。我把它设为空数组,文本文件是空字符串。但它不起作用。