2014-07-18 55 views
1

我有一個角度控制器,想在我的視圖中打開模態。如何從角度控制器打開模態

我的HTML

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 

    <div class="modal-dialog"> 

    <div class="modal-content"> 

     <div class="modal-header"> 

     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 

     <h4 class="modal-title" id="myModalLabel">Modal title</h4> 

     </div> 

     <div class="modal-body"> 

     ... 

     </div> 

     <div class="modal-footer"> 

     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 

     <button type="button" class="btn btn-primary">Save changes</button> 

     </div> 

    </div> 

    </div> 

</div> 


Settings Controller 

function SettingsController($scope,WalletManager,Storage){ 


    $scope.pageClass = 'page-settings'; 


    ]; 



$scope.myModal = function() { 

     var modalInstance = $myModal.open({ 


     }); 

如何從控制器打開?

+5

新建角..需要儘快做到這一點,而無需學習如何角作品......我不明白的人誰不利於任何一件事積極,只是去發表愚蠢的評論......目的是什麼? –

回答

7

按照Soren的建議,UI bootstrap是要走的路。以下是他們通過AngularJS打開模版的文檔示例。

而且,一個link他們的Plunker使用下面的代碼。

<html> 
<head> 

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script> 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script> 
    <script src="example.js"></script> 
    <link href= 
    "//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel= 
    "stylesheet"> 

    <title></title> 
</head> 

<body> 
    <div> 
     <script id="myModalContent.html" type="text/ng-template"> 
<div class="modal-header"> 
      <h3 class="modal-title">I'm a modal!</h3> 
     </div> 
     <div class="modal-body"> 
      <ul> 
       <li ng-repeat="item in items"> 
        <a ng-click="selected.item = item">{{ item }}</a> 
       </li> 
      </ul> 
      Selected: <b>{{ selected.item }}</b> 
     </div> 
     <div class="modal-footer"> 
      <button class="btn btn-primary" ng-click="ok()">OK</button> 
      <button class="btn btn-warning" ng-click="cancel()">Cancel</button> 
     </div> 
     </script> <button class="btn btn-default">Open me!</button> 
     <button class="btn btn-default">Large modal</button> <button class= 
     "btn btn-default">Small modal</button> 

     <div> 
      Selection from a modal: {{ selected }} 
     </div> 
    </div> 
</body> 
</html> 

而且,JavaScript的:

angular.module('plunker', ['ui.bootstrap']); 
var ModalDemoCtrl = function ($scope, $modal, $log) { 

    $scope.items = ['item1', 'item2', 'item3']; 

    $scope.open = function (size) { 

    var modalInstance = $modal.open({ 
     templateUrl: 'myModalContent.html', 
     controller: ModalInstanceCtrl, 
     size: size, 
     resolve: { 
     items: function() { 
      return $scope.items; 
     } 
     } 
    }); 

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

var ModalInstanceCtrl = function ($scope, $modalInstance, items) { 

    $scope.items = items; 
    $scope.selected = { 
    item: $scope.items[0] 
    }; 

    $scope.ok = function() { 
    $modalInstance.close($scope.selected.item); 
    }; 

    $scope.cancel = function() { 
    $modalInstance.dismiss('cancel'); 
    }; 
}; 
2

看看angular-bootstrap,它可以完成所有模式,再加上一些更有趣的效果,並且可以使用bower進行安裝。

該模塊附帶示例顯示如何從控制器調用它。