1

我的angularjs bootstrap模式未打開。爲什麼Angularjs bootstrap模式不能打開?

var app=angular.module('test', ['ui.bootstrap']); 
 
app.controller('ModalDemoCtrl', function($scope, $log,$modal) { 
 
    
 
    $scope.user = { 
 
     user: 'name', 
 
     password: null, 
 
     notes: null 
 
    }; 
 

 
    $scope.open = function() { 
 

 
     $modal.open({ 
 
      templateUrl: 'myModalContent.html', // loads the template 
 
      backdrop: true, // setting backdrop allows us to close the modal window on clicking outside the modal window 
 
      windowClass: 'modal', // windowClass - additional CSS class(es) to be added to a modal window template 
 
      controller: function ($scope, $modalInstance, $log, user) { 
 
       $scope.user = user; 
 
       $scope.submit = function() { 
 
        $log.log('Submiting user info.'); // kinda console logs this statement 
 
        $log.log(user); 
 
        $modalInstance.dismiss('cancel'); // dismiss(reason) - a method that can be used to dismiss a modal, passing a reason 
 
       } 
 
       $scope.cancel = function() { 
 
        $modalInstance.dismiss('cancel'); 
 
       }; 
 
      }, 
 
      resolve: { 
 
       user: function() { 
 
        return $scope.user; 
 
       } 
 
      } 
 
     });//end of modal.open 
 
    }; // end of scope.open function 
 
\t 
 
\t 
 
});
<html ng-app="test"> 
 
    <head> 
 
    <script src="https://code.angularjs.org/1.2.18/angular.js"></script> 
 
    <!--<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script>--> 
 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-2.5.0.js"></script> 
 
     
 
    <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"> 
 
    </head> 
 
    <body> 
 

 
<div ng-controller="ModalDemoCtrl"> 
 
    <script type="text/ng-template" id="myModalContent.html"> 
 
     <div class="modal-header"> 
 
      <h3>I'm a modal!</h3> 
 
     </div> 
 
     <form ng-submit="submit()"> 
 
      <div class="modal-body"> 
 
      <label>User name</label> 
 
      <input type="text" ng-model="user.user" /> 
 
      <label>Password</label> 
 
      <input type="password" ng-model="user.password" /> 
 
      <label>Add some notes</label> 
 
      <textarea rows="4" cols="50" ng-model="user.notes"></textarea> 
 
      </div> 
 
      <div class="modal-footer"> 
 
       <button class="btn btn-warning" ng-click="cancel()">Cancel</button> 
 
       <input type="submit" class="btn primary-btn" value="Submit" /> 
 
      </div> 
 
     </form> 
 
    </script> 
 

 
    <button class="btn" ng-click="open()">Open me!</button> 
 
    <div ng-show="selected">Selection from a modal: {{ selected }}</div> 
 
</div> 
 
    </body> 
 
</html>

如果我用bootstrap版本0.6.0模態工作fine.But如果我使用2.5.0版它給我的錯誤「未知提供商:$ modalProvider < - $模式」。但是我必須使用2.5.0版本。如何解決這個問題?請幫助。感謝先進。

回答

0

在JS部分: plunker here. 請在單獨的文件夾這兩個文件,並確保JS加載正確。

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

    $scope.user = { 
     user: 'name', 
     password: null, 
     notes: null 
    }; 

    $scope.open = function() { 

     $modal.open({ 
      templateUrl: 'myModalContent.html', // loads the template 
      backdrop: true, // setting backdrop allows us to close the modal window on clicking outside the modal window 
      windowClass: 'modal', // windowClass - additional CSS class(es) to be added to a modal window template 
      controller: function ($scope, $modalInstance, $log, user) { 
       $scope.user = user; 
       $scope.submit = function() { 
        $log.log('Submiting user info.'); // kinda console logs this statement 
        $log.log(user); 
        $modalInstance.dismiss('cancel'); // dismiss(reason) - a method that can be used to dismiss a modal, passing a reason 
       } 
       $scope.cancel = function() { 
        $modalInstance.dismiss('cancel'); 
       }; 
      }, 
      resolve: { 
       user: function() { 
        return $scope.user; 
       } 
      } 
     });//end of modal.open 
    }; // end of scope.open function 
}; 

在HTML中:

<!doctype html> 
<html ng-app="plunker"> 
    <head> 
    <script src="https://code.angularjs.org/1.2.18/angular.js"></script> 
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js"></script> 
    <script src="example.js"></script> 
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet"> 
    </head> 
    <body> 

<div ng-controller="ModalDemoCtrl"> 
    <script type="text/ng-template" id="myModalContent.html"> 
     <div class="modal-header"> 
      <h3>I'm a modal!</h3> 
     </div> 
     <form ng-submit="submit()"> 
      <div class="modal-body"> 
      <label>User name</label> 
      <input type="text" ng-model="user.user" /> 
      <label>Password</label> 
      <input type="password" ng-model="user.password" /> 
      <label>Add some notes</label> 
      <textarea rows="4" cols="50" ng-model="user.notes"> 

</textarea> 
      </div> 
      <div class="modal-footer"> 
       <button class="btn btn-warning" ng-click="cancel()">Cancel</button> 
       <input type="submit" class="btn primary-btn" value="Submit" /> 
      </div> 
     </form> 
    </script> 

    <button class="btn" ng-click="open()">Open me!</button> 
    <div ng-show="selected">Selection from a modal: {{ selected }}</div> 
</div> 
    </body> 
</html> 
+0

其實我不是故意的it.You正在使用的用戶界面,引導-TPLS-0.6.0.js。在plunker中使用這個js版本以及代碼按預期運行。但是如果您使用而不是給出錯誤的相同代碼。我需要使用ui-bootstrap-tpls-2.5.0.js –

+0

Man,檢查js的js優先級 –

相關問題