![](/img/trans.png)
[英]AngularJS - Dynamic DOM manipulation without hardcoding dom id's in the controller
[英]AngularJS - Separate controller and DOM manipulation
我正在阅读角度文档:
https://docs.angularjs.org/guide/controller
它有一点说:
不要使用控制器:
操纵DOM - 控制器应仅包含业务逻辑。 将任何表示逻辑放入控制器会显着影响其可测试性。 Angular对大多数情况和指令进行数据绑定以封装手动DOM操作。
所以我有以下代码,效果很好。
$scope.processForm = function() {
// Get and reset the captcha, then check it was valid.
response = grecaptcha.getResponse();
$scope.recaptchaValid = false;
grecaptcha.reset();
if (response.length == 0) {
alert("Not sure how you got here, but you shouldn't have. Go away!");
return;
}
// Update the user we are attempting to send the message.
$(".contact-message").text("Sending message...").removeClass("hidden alert-danger").addClass("alert-success");
$(".contact-container").addClass("hidden");
// Add the recaptcha data to the form so we can send it to the server easier
$scope.formData.captcha = response;
$http({
method : 'POST',
url : '/api/sendEmail.php',
data : $.param($scope.formData), // pass in data as strings
headers : {
// set the headers so angular passing info as form data (not request payload)
'Content-Type' : 'application/x-www-form-urlencoded'
}
}).success(function(data) {
if (!data.success) {
// If the server said there was an error, re-enable the form and display the error
console.error(data);
$(".contact-container").removeClass("hidden");
$(".contact-message").text(data.message).removeClass("hidden alert-success").addClass("alert-danger");
} else {
// If the server said all was good, display the message from the server
$(".contact-message").text(data.message).removeClass("hidden alert-danger").addClass("alert-success");
}
});
};
从像纯粹主义者这样的解释性禅,我看到做后端API调用的处理可能应该在某个地方的服务中关闭(虽然不知道如何做到这一点),但我不确定我是如何与$ scope中的数据“正确”,是否可以在那里进行DOM操作? 我已经减少了DOM操作来管理类,但是发送设置更新,我认为应该是它自己驱动一些范围数据并使用ng-hide。 我发现,如果您选择语言,这可能非常有用,但在维护时会使HTML页面变得庞大而繁琐。
任何帮助赞赏。
根据我的去向和@ C14L的注释,我将Angular代码的代码更新为:
$scope.processForm = function() {
// Get and reset the captcha, then check it was valid.
response = grecaptcha.getResponse();
$scope.recaptchaValid = false;
grecaptcha.reset();
if (response.length == 0) {
alert("Not sure how you got here, but you shouldn't have. Go away!");
return;
}
// Update the user we are attempting to send the message.
$scope.sendingMessage = true;
// Add the recaptcha data to the form so we can send it to the server easier
$scope.formData.captcha = response;
$http({
method : 'POST',
url : '/api/sendEmail.php',
data : $.param($scope.formData), // pass in data as strings
headers : {
// set the headers so angular passing info as form data (not request payload)
'Content-Type' : 'application/x-www-form-urlencoded'
}
}).success(function(data) {
$scope.serverMessage = data.message;
$scope.sendingStatus = data.success;
$scope.sendingMessage = false; // no longer sending
});
};
我不得不承认这确实更有效。 HTML虽然有点古怪:
<div class='sending-indicator alert alert-info' data-ng-hide="!sendingMessage">
<p>Sending message...</p>
</div>
<div class="alert contact-message"
data-ng-class="sendingStatus?'alert-success':'alert-danger'"
data-ng-hide="serverMessage.length == 0">{{serverMessage}}</div>
<div class="contact-container"
data-ng-hide="sendingMessage || (sendingStatus && serverMessage.length > 0)">
...
以这些线为例
// Update the user we are attempting to send the message.
$(".contact-message").text("Sending message...").removeClass("hidden alert-danger").addClass("alert-success");
$(".contact-container").addClass("hidden");
你可以像这样写它们。 在模板中
<div class="contact-message" ng-if="sendingMessage">Sending message</div>
<div class="contact-container" ng-if="!sendingMessage">Not sending</div>
并在控制器中
$scope.sendingMessage = true; // now it shows "sending"
$scope.sendingMessage = false; // now "not sending"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.