简体   繁体   English

将值分配给Angular-ui模态

[英]Assign values to Angular-ui modal

I think i have some pretty big holes in my code, as when the modal is appearing, the content from the table (which when you click on a row produces the modal), is not populating the input boxes I have inside of the modal. 我认为我的代码中有很多漏洞,因为出现模态时,表中的内容(当您单击一行时会产生模态)并没有填充模态内部的输入框。 I think I'm tackling the situation in the wrong way and some direction would be fantastic. 我认为我以错误的方式解决了这种情况,并且某些方向将是很棒的。

My JS: 我的JS:

var app = angular.module('peopleInformation', ['ngAnimate','ui.bootstrap']);

app.controller('myCtrl', function($scope, $http, $uibModal) {

$http.get("Assignment005.json").success(function(response){
    $scope.myData = response.People;
});

$scope.modify = function(currentData){

    var modalInstance = $uibModal.open({
        animation: true,
        templateUrl: 'myModalContent.html',
        controller:function($scope, $uibModalInstance, details){
                $scope.FirstName = details.FirstName;
                $scope.LastName = details.LastName;
                $scope.Age = details.Age;
                $scope.Nickname = details.Nickname;

            $scope.update = function () {
                $uibModalInstance.dismiss('cancel');
            };
        },
        size: 'lg',
        resolve: {
            details: function() {
                return currentData;
            }
        }   
    });

};
}); 

My modal: 我的模态:

              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Your row of data</h4>
              </div>
              <div class="modal-body" name="modelData" style="height:200px">
                  <form class="form-horizontal pull-left form-width" role="form">
                      <div class="form-group">
                        <label class="control-label col-sm-4" for="first">First Name:</label>
                        <div class="col-sm-8">
                          <input type="text" class="form-control" id="first" ng-model="FirstName">
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="control-label col-sm-4" for="last">Last Name:</label>
                        <div class="col-sm-8"> 
                          <input type="text" class="form-control" id="last" ng-model="LastName">
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="control-label col-sm-4" for="age">Age:</label>
                        <div class="col-sm-8"> 
                          <input type="text" class="form-control" id="age" ng-model="Age">
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="control-label col-sm-4" for="nick">Nickname:</label>
                        <div class="col-sm-8"> 
                          <input type="text" class="form-control" id="nick" ng-model="Nickname">
                        </div>
                      </div>
                  </form>
              </div>
          <div class="modal-footer">
        <button type="button" class="btn btn-danger pull-left" data-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-success pull-right" data-dismiss="modal">Submit</button>
      </div>
    </div>

Main HTML in case it's needed: 需要时的主要HTML:

<body>
<div data-ng-app="peopleInformation" data-ng-controller="myCtrl" class="jumbotron">
  <div class="panel panel-default">
      <div class="panel-heading">Essential Information</div>
          <div class="table-responsive">
              <table class="table table-hover">
                  <thead>
                      <tr>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Age</th>
                        <th>Nickname</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr data-ng-repeat="details in myData" data-ng-click="modify(details)">
                        <td>{{ details.FirstName }}</td>
                        <td>{{ details.LastName }}</td>
                        <td>{{ details.Age }}</td>
                        <td>{{ details.Nickname }}</td>
                      </tr>
                  </tbody>                    
              </table>
              <button type="button" class="btn btn-info pull-right" data-ng-click="new()">Add
              </button>
          </div>
      </div>
  <div ng-include="myModalContent.html"></div>
 </div>
</body>

Im very new to using Angular so if you could be overtly simple with me that would help to clarify things, although again, any help is appreciated. 我对使用Angular非常陌生,因此,如果您对我过分简单,这将有助于澄清问题,尽管再次感谢您的帮助。

Bellow is the angular modal instance controller 波纹管是角度模态实例控制器

 app.controller('ModalInstanceCtrl', function ($scope,
 $uibModalInstance, item) {

   $scope.customer = item;

   $scope.yes = function () {
     $uibModalInstance.close();   };

   $scope.no = function () {
     $uibModalInstance.dismiss('cancel');   
  }; 
});

bellow is the code for call angular modal 波纹管是呼叫角模态的代码

 $scope.open = function (item) {
     var modalInstance = $uibModal.open({
       animation: true,
       scope: $scope,
       templateUrl: 'myModalContent.html',
       controller: 'ModalInstanceCtrl',
       size: 'md',
       resolve: {
         item: function () {
          return item;
         }
       }
     });

     modalInstance.result.then(function (selectedItem) {
        $log.info(selectedItem);
       });
     }, function () {
       $log.info('Modal dismissed at: ' + new Date());
     });   
   };

Bellow is code for template 波纹管是模板的代码

<script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
        <h3 class="modal-title">Re-calculate retail price</h3>
    </div>
    <div class="modal-body">
        Margin percent of selected customer is <b>{{ customer.margin_percent }}</b> <br />
        Do you want to recalculate the retail price?
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="yes()">Yes</button>
        <button class="btn btn-warning" type="button" ng-click="no()">No</button>
    </div>
</script>

In your HTML file you are passing different parameter to modify function, It should be equal to the parameter specified in ng-repeat directive. 在您的HTML文件中,您传递了不同的参数来修改函数,它应等于ng-repeat指令中指定的参数。 So in this case this: 因此,在这种情况下:

<tr data-ng-repeat="data in myData" data-ng-click="modify(details)">

will become: 会变成:

<tr data-ng-repeat="details in myData" data-ng-click="modify(details)">

I was actually assigning the values in the wrong place I believe. 我实际上是在错误的位置分配值,我相信。 I moved the: 我移动了:

$scope.FirstName = details.FirstName;

Outside of the var modalInstance variable, and they are now populating the input boxes. var modalInstance变量之外,它们现在正在填充输入框。 If this is messy or not standard then let me know as sometimes the right result is not always the right method. 如果这是混乱的还是不标准的,那么让我知道,因为有时正确的结果并不总是正确的方法。 Thanks for those that tried to help, much appreciated. 感谢您的帮助,不胜感激。

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

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