简体   繁体   English

如何在angular JS中将参数传递给模态

[英]How to pass parameter to modal in angular JS

Below is the current HTML code -- 以下是当前的HTML代码 -

    <div class="container">
      <h2>Basic Modal Example</h2>
      <!-- Trigger the modal with a button -->
      <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal 1</button>
      <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal 2</button>
      <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal 3</button>

      <!-- Modal -->
      <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">

          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
              <p>Content of modal -- </p>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>

        </div>
      </div>

    </div>

Here multiple buttons call the same modal. 这里有多个按钮调用相同的模态。 Now inside the modal there is no way to determine which button press has called this. 现在在模态内部无法确定哪个按钮按下了这个。

I want to modify the above code so that i can pass a parameter into the modal. 我想修改上面的代码,以便我可以将参数传递给模态。 For example, clicking on 1st button should pass "button 1" and that should be displayed in the modal body section. 例如,单击第一个按钮应该通过“按钮1”,它应该显示在模态主体部分中。

I know I can add ng-click to the buttons and call a function. 我知道我可以在按钮上添加ng-click并调用一个函数。 I can pass the string button 1/2/3 as its parameter. 我可以传递字符串button 1/2/3作为参数。 Inside the function I can set a particular global parameter value as the string passed. 在函数内部,我可以将特定的全局参数值设置为传递的字符串。 But how do I invoke the modal from inside this function? 但是如何从这个函数中调用模态? I am not sure whether this is even possible. 我不确定这是否可行。

Please let me know how I can achieve the above using angular JS. 请告诉我如何使用angular JS实现上述功能。

You can implement ng-clicks of each button and then can create a scope variable inside the function that gets triggered on click. 您可以实现每个按钮的ng-clicks,然后可以在单击时触发的函数内创建范围变量。 And by using the same variable you can now update the modal body section. 通过使用相同的变量,您现在可以更新模态体部分。

HTML Would be like: HTML就像:

<div class="container">
  <h2>Basic Modal Example</h2>
  <!-- Trigger the modal with a button -->
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" ng-click="data = 'content 1'">Open Modal 1</button>
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" ng-click="data = 'content 2'">Open Modal 2</button>
  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" ng-click="data = 'content 3'">Open Modal 3</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Content of modal -- {{data}} </p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

</div>

You don't need to use global things. 您不需要使用全局的东西。

You can do it using resolve property. 您可以使用resolve属性执行此操作。

defined the function which will going to call your modal function which open the modal. 定义了将调用打开模态的模态函数的函数。

<button ng-click="callModalFun('Btn1')">Btn1</button>
<button ng-click="callModalFun('Btn2')">Btn2</button>
<button ng-click="callModalFun('Btn3')">Btn3</button>

JS : JS:

function callModalFun(strBtnName){

   var modalInstance  =  modal.open({

        ..,
        resolve : {

            data : {

                strFromBtn : strBtnName;
            }
        },
        controller : 'myCtrl'
    });          
}

and in the controller do like this one. 并且在控制器中确实喜欢这个。

.controller('myCtrl',function($scope,resolveData){

    $scope.strBtnName = resolveData.data.strFromBtn;

})

strBtnName is available inside of the template. strBtnName在模板内部可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM