繁体   English   中英

AngularJS:将模态返回的数据绑定到从中单击的行

[英]AngularJS: Bind data returned from a modal to row it was clicked from

在这里使用AngularJS。

我正在使用具有下拉菜单的UI。 根据用户选择的内容,我向用户显示2个选项卡。

每个选项卡数据都是从服务返回的,该服务仅返回数据数组(字符串)。

针对返回的每个字符串值,我显示一个针对它的按钮。 当您单击按钮时,它将打开一个模式弹出窗口,用户可以在其中选择一些数据。 当他们关闭模态时,我将数据返回给控制器。

将数据绑定到选项卡,打开模式并从模式返回数据的正常流程都可以正常工作。

我无法理解或设计的是如何将返回的数据绑定到单击它的按钮或行上

例如如下:

Tab1

String1 - Button1

String2 - Button2

String3 - Button3

如果我通过单击button1打开模式,则如何找出button1被按下并绑定回从其模式返回的数据。

一些相关的代码如下:

<div id="params" ng-if="type.selected">
  <tabset class="tabbable-line">
    <tab heading="Sets" ng-if="sets" active="tab.set">    
        <div class="form-group m-grid-col-md-8 param" style="margin-top:5px"
             ng-repeat="set in sets" >
              <label class="control-label col-md-3 param-label">{{set}}
              </label>                  
              <button ng-click="openModal()" class="btn btn-info btn-sm">
                Select
              </button> 
        </div>
    </tab>
    <tab heading="Tables" ng-if="tables" active="tab.table">       
        <div class="form-group m-grid-col-md-8 param"
             ng-repeat="table in tables">
            <label class="control-label col-md-3 param-label">{{table}}
            </label>               
            <button ng-click="openModal()" class="btn btn-info btn-sm">
                Select
            </button>
            </div>
        </tab>
    </tabset>
</div> 

控制器:

  $scope.onChangeType = function (selectedValue) {           
       $scope.getData(selectedValue);
  };

  $scope.getData = function (selectedValue) {
      //Commenting out the service part for now and hardcoding array
      // service.getData(selectedValue).then(function (res) {
           $scope.sets = ['Set1', 'Set2', 'Set3'];   
           $scope.tables = ['Table1', 'Table2'];
      // });
  };


  $scope.openModal = function () {
       myFactory.defineModal().then(function (response) {
            //how to bind data from response
        });
   };

我为此示例创建了一个plnkr,如下所示: http ://plnkr.co/edit/vqtQsJP1dqGnRA6s?preview

--Edited--

 <div class="form-group m-grid-col-md-8 param" ng-repeat="table in tables">
    <label class="control-label col-md-3 param-label">{{table}}
    </label>               
    <button ng-click="openModal(table)" class="btn btn-info btn-sm">
        Select
    </button>
       <span>
        {{table.utype}}
    </span> 
</div>

table对象作为参数传递给openModal函数:

<button ng-click="openModal(table)">Select</button>

openModal函数中使用它:

$scope.openModal = function (table) {
     myFactory.defineModal().then(function (result) {
         table.utype = result.utype;
         table.minvalue = result.minvalue;
     });
};

确保使用结果关闭模式:

$scope.ok = function () {
    var result = { 
      utype: $scope.utype,
      minvalue: $scope.minvalue,
    };
    $modalInstance.close(result); 
};

请记住,模态被认为是破坏性的,并且被用户鄙视。

一般来说,干扰和分心会负面影响人类的表现,这是认知心理学中的常见发现。 许多研究表明,分心会大大增加各种任务的工作时间。

有关更多信息,请参见


虽然我没有收到任何错误,但是我没有得到返回的文本。

确保为ng-repeat提供对象:

  $scope.getData = function (selectedValue) {
      //Commenting out the service part for now and hardcoding array
      // service.getData(selectedValue).then(function (res) {
           ̶$̶s̶c̶o̶p̶e̶.̶t̶a̶b̶l̶e̶s̶ ̶=̶ ̶[̶'̶T̶a̶b̶l̶e̶1̶'̶,̶'̶T̶a̶b̶l̶e̶2̶'̶]̶;̶
           $scope.tables = [
             {name:'Table1',},
             {name:'Table2',},
          ];
      // });
  };

PLNKR上演示

尝试将table传递给模板中的openModal

<button ng-click="openModal(table)"

现在,您可以在openModal函数openModal其用作参考

$scope.openModal = function (table) {
  // table === the clicked table
}

暂无
暂无

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

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