gpt4 book ai didi

javascript - 如何在angularjs中将范围变量设置为空

转载 作者:行者123 更新时间:2023-12-02 14:11:31 25 4
gpt4 key购买 nike

这是我的代码。

$scope.sendFeedback = function ()  {

var mobile1 = $scope.mobile;
var feedback1 = $scope.feedback;

var userfeedback = {
mobile: mobile1,
feedback: feedback1
};

var feedbackRef = firebase.database().ref("feedback/");
var newFeedbackRef = feedbackRef.push();
newFeedbackRef.set(userfeedback, function(error) {
if (error) {
alert ('Error while registering feedback.');
} else {
$scope.feedback = '';
alert ('Your feedback is registered.');

}
});
}

调用此函数后,我可以看到警报(“您的反馈已注册。”);但 $scope.feedback = '' 没有将该特定文本字段设置为空。我尝试了 $scope.feedback = null;也,但没有运气。

这里出了什么问题?

编辑:添加了 HTML View 。

<md-input-container class="md-block">
<label>Feedback</label>
<input required="" name="feedback" ng-model="feedback" >
<div ng-messages="projectForm.feedback.$error" role="alert">
<div ng-message-exp="['required']">
This field is required.
</div> </div>

</md-input-container>

最佳答案

我多次遇到此类问题,并使用以下解决方法解决了它:

    angular.element(document.getElementById('doc'))
.scope().$apply( function() {
$scope.feedback=''
)}

为了测试 SO 的代码片段运行器,我制作了一个非常小的工作示例,并且主要使用您自己的代码。希望这会有所帮助。

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>

<script>
var module=angular.module('test',[]).controller('test', function($scope)
{
$scope.click = function() {
$scope.feedback = '';
alert ('Your feedback is registered.');
}
})
</script>
</head>
<body>

<div ng-app="test" ng-controller="test" id='feedback'>
<md-input-container class="md-block">
<label>Feedback</label>
<input required="" name="feedback" ng-model="feedback" >
<div ng-messages="projectForm.feedback.$error" role="alert">
<div ng-message-exp="['required']">
This field is required.
</div> </div>

<button ng-click='click();'>click?</button>
</md-input-container>
</div>

关于javascript - 如何在angularjs中将范围变量设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39521940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com