【发布时间】:2018-03-14 10:20:41
【问题描述】:
我正在尝试在 SweetAlert 中构建登录/注册表单。
一切正常,但我正在尝试使用由数组中的 ng-repeat 组成的选项进行选择。
这是我的代码:
我的innerHTML的一部分:
var registerForm = document.createElement("div");
registerForm.innerHTML = "<form><div class=\"row\"><div class=\"col-lg-6\"><label style=\"float: left;\" for=\"firstname\"><b>Firstname:</b></label><input type=\"text\" id=\"firstname\" class=\"form-control\">
<label style=\"float: left;\" for=\"lastname\"><b>Lastname:</b></label><input type=\"text\" id=\"lastname\" class=\"form-control\">
<label style=\"float: left;\" for=\"login\"><b>Login:</b></label><input type=\"text\" id=\"login\" class=\"form-control\">
<label style=\"float: left;\" for=\"password\"><b>Password:</b></label><input type=\"password\" id=\"password\" class=\"form-control\">
<label style=\"float: left;\" for=\"password-again\"><b>Repeat Password:</b></label><input type=\"password\" id=\"password-again\" class=\"form-control\"></div>
<div class=\"col-lg-6\"><label style=\"float: left;\" for=\"email\"><b>E-Mail:</b></label><input type=\"text\" id=\"email\" class=\"form-control\">
<label style=\"float: left;\" for=\"city\"><b>City:</b></label><input type=\"text\" id=\"city\" class=\"form-control\">
<label style=\"float: left;\" for=\"postal\"><b>Postal Code:</b></label><input type=\"text\" id=\"postal\" class=\"form-control\">
<label style=\"float: left;\" for=\"adress\"><b>Adress:</b></label><input type=\"text\" id=\"adress\" class=\"form-control\">
<label style=\"float: left;\" for=\"country\"><b>Country:</b></label><select class=\"form-control\" ng-model=\"countires\" required><option ng-repeat=\"item in countries\" value=\"{{item.id}}\">{{item.name}}</option></select>
</div>
<div class=\"col-lg-12\"><p style=\"float:left;\">Fields marked with <span class=\"redstar\"><b>*</b></span> are required.</p></div></div></form>";
我的 swal 以及我如何将我的 html 放入其中:
$scope.registerSwal = function(){
swal({
title: 'Sign Up',
text: 'Create a new account.',
content: registerForm,
buttons: {
stop: {
text: "Cancel",
className: "red-modal-button",
},
ok: {
text: "Register",
value: "ok",
className: "green-modal-button",
},
}
});
}
我的国家/地区数组:
$scope.countries = {1 : {name: 'Poland', id: 1}, 1 : {name: 'Holland', id: 2}};
问题是我在innerHTML 的选择选项中的ng-repeat 不起作用。有什么办法可以吗?
【问题讨论】:
-
为什么要使用 DOM 操作以及如何/在哪里使用它?
-
我这样做是因为 SweetAlert 内容不接受 html。所以我制作了我的 innerHTML 并将其作为我的 swal 的内容。它工作得很好,但 innerHTML 不读取我的 angularjs ng-repeat。我将在我的问题中添加屏幕截图。
-
所以,不要使用 ng-repeat,而是使用 ng-options。 请记住,您需要首先在控制器文件中初始化国家/地区,例如 $scope.countries = [id: 1, name: ''India"];
-
@Surjeet Bhadauriya 也不起作用,因为它是一个 angularjs 标签,innerHTML 不读取它。
标签: javascript angularjs sweetalert2