在angular.ready函数上设置表单属性

时间:2014-11-12 15:30:58

标签: javascript jquery html5 angularjs angular-ui

我的模态中有一个表单(angular ui)。我需要从我的控制器设置表单的属性。

我的HomepageController:

var iframeModal = $modal.open({
     templateUrl: 'immdModal.html',
     controller: 'immdController',
     scope: $scope
});
iframeModal.result.then(function(selectedItem) {
  $scope.selected = selectedItem;
 }, function() {
});

我有:

<div id="immdPwdResetFrame">
    <form id="myForm"></form>
    <iframe name="hidden_form_for_iframe"></iframe>
</div>
<script>
    $(document).ready(function() {
        document.forms["myForm"].submit();
    });
</script>

在我的模态HTML(immdModal.HTML)中,我有

angular.element(document).ready(function() {
    var form = document.getElementById("myForm");
    form.setAttribute("method", "post");
});

在我的immdController控制器中。但是我在控制台中得到错误:form is undefined。 如何从控制器设置表单的属性?

1 个答案:

答案 0 :(得分:0)

免责声明:试图给出一个看似奇怪的问题的hacky变通方法:如何在表单上设置属性。这个解决方案正好使用angular。

您总是可以为表单定义一个指令来执行DOM操作,如:

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

// Given that the myapp is properly referenced in the HTML (i.e. <html ng-app="myapp">) 
// this will execute on any element with an attribute named 'immd-modal-form'
app.directive("immdModalForm",function(){

  return {
    restrict: 'A',
    link: function(scope, element, attrs) {

      element.attr("method", "post");

    }
  };
});

然后

<form immd-modal-form></form>

以这种方式对modal形式使用angular.ready()/ $(document).ready()会遇到麻烦。这些事件在页面的初始加载时使用,而不是模态初始化。

我相信调用&#34; submit()&#34;在表单上可能会踢出你的页面(重定向)。你的问题有很多问题。你真的需要告诉我们你想要实现的目标。我确信有一种更简单的方法可以做到这一切!