【问题标题】:AngularJS in innerHTML with SweetAlert带有 SweetAlert 的 innerHTML 中的 AngularJS
【发布时间】: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


【解决方案1】:

不要在没有 directive 的情况下在 AngularJS 中操作 DOM:

在高层次上,指令是 DOM 元素(例如属性、元素名称、注释或 CSS 类)上的标记,它告诉 AngularJS 的 HTML 编译器($compile)将指定的行为附加到该 DOM 元素(例如,通过事件监听器),甚至转换 DOM 元素及其子元素。

注意: $scope.countries 不是数组。我把它变成了一个数组。

查看

<div ng-controller="MyCtrl">
  <my-directive countries="countries"></my-directive>
</div>

AngularJS 应用程序:

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.countries = [
      {name: 'Poland', id: 1},
      {name: 'Holland', id: 2}
    ];
});

myApp.directive('myDirective', function () {
    return {
      restrict: 'E',
      scope: {
        countries: '='
      },
      template: `<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>` 
    }
});

> Demo fiddle

【讨论】:

  • 好吧。我明白了。非常感谢您的回复。但是我现在如何将它用作我的 SweetAlert 内容?
  • @W.Lukasiewicz 更新 + 添加了一个演示小提琴。现在你的 ng-repeat 确实有效。我希望这会对你有所帮助。
  • @Iin。就像我说的。谢谢 :) 它适用于我的 html 视图。但我现在不能真正将它设置为我的 SweetAlert 的内容,这就是我想要做的。
  • @W.Lukasiewicz 你应该使用github.com/oitozero/ngSweetAlert,因为它支持AngularJS。您还可以创建一个指令来处理 SweetAlert 的本机功能。
  • @Iin 这对我的进一步项目很有帮助,但我没有修复 sweetalert 中的表单,只是进入了引导程序的正常模式窗口。
【解决方案2】:

您需要将registerForm 编译到作用域中才能在作用域上使用ng-repeat

$compile(registerForm)($scope);

【讨论】:

    【解决方案3】:

    更改对象中的重复键即可。

    $scope.countries = {1 : {name: 'Poland', id: 1}, 2 : {name: 'Holland', id: 2}};
    

    演示:https://plnkr.co/edit/pE6EGWS5oV4WUEzCzrCr?p=preview

    【讨论】:

    • 这是一个错误,但这不是它在 innerHTML 中不起作用的原因。
    猜你喜欢
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2012-10-11
    • 2017-07-04
    • 2019-12-21
    • 2015-10-06
    相关资源
    最近更新 更多