简体   繁体   中英

send the id from the table to the controller with angularjs

I wonder how can I send the id of the selected row from my table to the controller when I try to show the details of these row,this is my code:

app.js:

.controller("etudmodifCtrl", ["$scope", "$http", "filterFilter", "$rootScope", "logger", "$filter", "$modal", "$log", function ($scope, $http, filterFilter, $rootScope, logger, $filter, $modal, $log) {

$http({
    method: 'GET',
    url: 'http://localhost:50001/api/Students/' + $scope.store.id
}).success(function (data) {
    $scope.firstname = data.FirstName;
    $scope.lastname = data.LastName;
    $scope.email = data.Email;
    console.log("success");
}).error(function (data, status, headers, config) {
    console.log("data error ...");
});

$scope.open = function () {
    var modalInstance;
    modalInstance = $modal.open({
        templateUrl: "myModalContent1.html",
        controller: "ModalInstanceCtrl",
        resolve: {
            items: function () {
                return $scope.items
            }
        }
    }), modalInstance.result.then(function (selectedItem) {
        $scope.selected = selectedItem
    }, function () {
        $log.info("Modal dismissed at: " + new Date)
    })
}

}])

my html code:

    <table class="table table-responsive table-hover" ng-controller="etudmodifCtrl">
        <tr ng-repeat="store in currentPageStores">
            <td align="center">{{store.LastName}}</td>
            <td align="center">{{store.FirstName}}</td>
            <td align="center">{{store.Email}}</td>
            <td align="center">{{store.Class}}</td>
            <td align="center">
            <script type="text/ng-template" id="myModalContent.html">
            < div class ="modal-header">Modify Informations</div><div class = "modal-body modal-dialog modal-lg3" data-ng-controller = "etudmodifCtrl">

  <div class="panel-body" >
       <div class="col-md-6">
           <div class="form-group">
             <label for="" class="col-sm-2">FirstName</label>< div class = "col-sm-10"><input type = "email" class ="form-control" ng-model = "firstname"> 
</div>
            </div >< /div>
            <div class="form-group">
             <label for="" class="col-sm-2">LastName</label>< div class = "col-sm-10"><input type = "email" class ="form-control" ng-model = "lastname"> 
</div>
            </div >< /div>
                     <div class="form-group">
             <label for="" class="col-sm-2">Email</label>< div class = "col-sm-10"><input type = "email" class ="form-control" ng-model = "email"> 
</div>
            </div >< /div>                                
                </script>
            </td>
        </tr>
    </table>
    <button type="button" onclick="window.location = '" aria-label="Center Align" ng-click="open()" data-toggle="modal" data-target=".bs-example-modal-lg">Modify</button>

I try to send the id via the open function but I get a syntax error thanks for help

In button bg-click pass the id by using ng-click="open(store.storeId)" complete code

<button type="button" 
  aria-label="Center Align" 
  ng-click="open(store.storeId)" 
  data-toggle="modal" 
  data-target=".bs-example-modal-lg">Modify</button>

And change your open function to accept a parameter like this

 $scope.open = function (id) {
   ............
   ............
 }

for more detail, who to pass value from controller to model controller see this Pass parameter to modal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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