如何在angularjs中获取用户输入并将其从一个模态传递到另一个模态?如何在javascript中连接控制器?

时间:2022-06-21 07:07:18

I am new to Javascript and Angular....and I am trying to change a code so that when the user click on a button, it shows a form to user to enter her/his name, then if they press submit button Smilar to this image , the result should be saved.

我是Javascript和Angular的新手....我正在尝试更改代码,以便当用户点击按钮时,它会向用户显示一个表单以输入她/他的名字,然后如果他们按下提交按钮Smilar这个图像,结果应该保存。

So I have a main controller and view (let's say its name is page), when the first button is clicked, a function will be called that make the form visible:

所以我有一个主控制器和视图(让我们说它的名字是页面),当点击第一个按钮时,将调用一个使表单可见的函数:

mainController.controller('Page', ['$scope','ModalService' ,
  function($scope,ModalService) {  

  $scope.clickHere = function () {
    ModalService.showModal({
        templateUrl: "angular/views/modals/myForm.html",
        controller:  "myFormCtrl"
    }).then(function(modal) {

        //it's a bootstrap element, use 'modal' to show it
        modal.element.modal();
        modal.close.then(function(result) {
            if(result=='submit')
            {

            }
        });
    });        
} 

and the form controller looks like this:

表单控制器如下所示:

mainController.controller('myFormCtrl', ['$scope','close',
      function($scope,close) {

        $scope.test = angular.element(document.getElementById("username")).val()
        console.log($scope.test)

        //$scope.test = username.getText()
        $scope.close = function(result) {

            console.log($scope.test)
            close(result, 500);
        };

    }]);

Now I am very confused, I don't know how to pass the name of the user to the Page so I can use it for in other functions that I have...I want the name of the user to be passed when Submit is selected and nothing happens otherwise.

现在我很困惑,我不知道如何将用户名传递给Page,所以我可以在其他函数中使用它...我希望在提交时传递用户的名字选择,否则没有任何反应。

Just a node: All controllers and Models and HTML codes are connected...I see the form and I can click on submit...but I cannot save the result.

只是一个节点:所有控制器和模型和HTML代码都已连接......我看到表单,我可以点击提交...但我无法保存结果。

Thanks in advance for your help

在此先感谢您的帮助

1 个解决方案

#1


0  

Information can be shared across controllers using services. You can create a variable in the ModalService, inject ModalService into the controller of the form and set that variable with the name entered by user. Then, you can access this variable in parent controller via already injected ModalService

可以使用服务在控制器之间共享信息。您可以在ModalService中创建一个变量,将ModalService注入到表单的控制器中,并使用用户输入的名称设置该变量。然后,您可以通过已注入的ModalService在父控制器中访问此变量

#1


0  

Information can be shared across controllers using services. You can create a variable in the ModalService, inject ModalService into the controller of the form and set that variable with the name entered by user. Then, you can access this variable in parent controller via already injected ModalService

可以使用服务在控制器之间共享信息。您可以在ModalService中创建一个变量,将ModalService注入到表单的控制器中,并使用用户输入的名称设置该变量。然后,您可以通过已注入的ModalService在父控制器中访问此变量