2016-04-26 137 views
0

我正在使用angularjs。我正在嘗試將參數和對象發送到彈出窗口中。Angularjs +模型+傳遞參數和對象

Click here:

HTML代碼:

<script src="https://code.angularjs.org/1.4.9/angular.min.js"></script> 
<script src="https://rawgit.com/dwmkerr/angular-modal-service/master/dst/angular-modal-service.js"></script> 

<div class="container" ng-app="app" ng-controller="Controller"> 

    <h3>Angular Modal Service</h3> 
    <a class="btn btn-default" href ng-click="show()">Show a Modal</a> 
    <p>{{message}}</p> 

    <!-- The actual modal template, just a bit o bootstrap --> 
    <script type="text/ng-template" id="modal.html"> 
     <div class="modal fade"> 
      <div class="modal-dialog"> 
      <div class="modal-content"> 
       <div class="modal-header"> 
       <button type="button" class="close" ng-click="close('Cancel')" data-dismiss="modal" aria-hidden="true">&times;</button> 
       <h4 class="modal-title">Yes ossssssssr No?</h4> 
       </div> 
       <div class="modal-body"> 
       <p>It's your call...</p> 
       </div> 
       <div class="modal-footer"> 
       <button type="button" ng-click="close('No')" class="btn btn-default" data-dismiss="modal">No</button> 
       <button type="button" ng-click="close('Yes')" class="btn btn-primary" data-dismiss="modal">Yes</button> 
       </div> 
      </div> 
      </div> 
     </div> 
    </script> 

</div> 

JS代碼:

var app = angular.module('app', ['angularModalService']); 

app.controller('Controller', function($scope, ModalService) { 

    $scope.url = 'google.com'; 
    var obj= {}; 
    obj.name = "test" 
    $scope.data = obj; 

    $scope.show = function() { 
     ModalService.showModal({ 
      templateUrl: 'modal.html', 
      controller: "ModalController" 
     }).then(function(modal) { 
      modal.element.modal(); 
      modal.close.then(function(result) { 
       $scope.message = "You said " + result; 
      }); 
     }); 
    }; 

}); 

app.controller('ModalController', function($scope, close) { 

$scope.close = function(result) { 
    close(result, 500); // close, but give 500ms for bootstrap to animate 
}; 

}); 

如何通過範圍URL和數據值進入彈出。 我看到幾個例子傳遞使用決心。

請檢查我的例子

回答

1

試試這個:

  ModalService.showModal({ 
       templateUrl: 'modal.html', 
       controller: "ModalController", 
       resolve : { 
        data: function() { 
         return $scope.data 
        } 
       } 
      }) 

模型控制器,你可以得到這樣的數據給你的模式

app.controller('ModalController', function($scope, close, data) { 

$scope.close = function(result) { 
    close(result, 500); // close, but give 500ms for bootstrap to animate 
}; 

}); 
+0

我想它不工作 – RSKMR

1

呼叫廣播和捕捉事件數據:

var app = angular.module('app', ['angularModalService']); 

app.controller('Controller', function($scope, $rootScope, ModalService) { 

    $scope.url = 'google.com'; 
    var obj= {}; 
    obj.name = "test" 
    $scope.data = obj; 

    $scope.show = function() { 
     ModalService.showModal({ 
      templateUrl: 'modal.html', 
      controller: "ModalController" 
     }).then(function(modal) { 
      modal.element.modal(); 
      modal.close.then(function(result) { 
       $rootScope.$broadcast('ModalController:set', {message: "You said " + result}); 
      }); 
     }); 
    }; 

}); 

app.controller('ModalController', function($scope, close) { 

$scope.close = function(result) { 
    close(result, 500); // close, but give 500ms for bootstrap to animate 
}; 

    $scope.$on('ModalController:set', function(event, data) { 
    for(var i in data) { 
     $scope[i] = data[i]; 
    } 
    }); 

}); 
0

的ShowModal」與名「決心」。它讓你分享(通過)參數的controller.For例如一個屬性:

ModalService.showModal({ 
     templateUrl: 'modal.html', 
     controller: "ModalController", 
     resolve : { 
      data: function() { // ro you can pass a value in stead of function 
       return $scope.data 
      } 
     } 
    }) 

並在控制器,你可以注入它像服務範圍使用它像scope.data