2015-09-18 65 views
0

我使用從here使用的用戶界面,引導模式

角度UI-bootsrap模式當我點擊Open me按鈕,我什麼也沒得到。彈出窗口不顯示。我的代碼可以在下面找到。

app.js

(function() { 

     'use strict'; 
     angular.module('probaApp', ['ui.router','ngAnimate','ui.bootstrap','ui.bootstrap.modal']) 
      .config(function ($stateProvider, $urlRouterProvider) { 

       $urlRouterProvider.otherwise("/home"); 

       $stateProvider 
        .state('home', { 
         url: '/home', 
         templateUrl: 'src/home.tpl.html', 
         controller: 'modalController' 
        }) 

      }); 
    })(); 

的index.html

<!DOCTYPE html> 
<html lang="en" ng-app="probaApp"> 
<head> 
    <meta charset="UTF-8"> 
    <link type="text/css" rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css"> 
    <title></title> 
</head> 
<body> 

<ui-view></ui-view> 

<script src="bower_components/angular/angular.js"></script> 
<script src="bower_components/angular-bootstrap/ui-bootstrap.js"></script> 
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script> 
<script src="bower_components/angular-animate/angular-animate.js"></script> 
<script src="src/app.js"></script> 
<script src="src/modal-controller.js"></script> 
<script src="src/modal-instance-controller.js"></script> 

</body> 
</html> 

模態-controller.js

 (function() { 
    'use strict'; 

    function ModalController($scope,$modal,$log) { 

     $scope.modalController = {}; 

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

     $scope.modalController.animationsEnabled = true; 

     $scope.modalController.open = function(size){ 

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

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

     }; 

     $scope.modalController.toggleAnimation = function(){ 
      $scope.modalController.animationsEnabled = !$scope.modalController.animationsEnabled; 
     }; 


    } 

    angular.module('probaApp').controller('modalController', ModalController); 
})(); 

模態-實例 - 控制器

/** 
* Created by user on 19.09.2015. 
*/ 
(function(){ 

    function ModalInstanceController ($scope,$modalInstance,items){ 

     $scope.modalInstanceController = {}; 

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

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

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

    angular.module('probaApp').controller('modalInstanceController',ModalInstanceController); 
})(); 

home.tpl

<div> 
    <script type="text/ng-template" id="myModalContent.html"> 
     <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 href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a> 
       </li> 
      </ul> 
      Selected: <b>{{ selected.item }}</b> 
     </div> 
     <div class="modal-footer"> 
      <button class="btn btn-primary" type="button" ng-click="ok()">OK</button> 
      <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button> 
     </div> 
    </script> 

    <button type="button" class="btn btn-default" ng-click="modalController.open()">Open me!</button> 
</div> 

編輯狂

TypeError: Cannot read property 'then' of undefined 
    at Object.ModalController.$scope.modalController.open (http://localhost:63342/Proba/src/modal-controller.js:26:42) 
    at fn (eval at <anonymous> (http://localhost:63342/Proba/bower_components/angular/angular.js:13275:15), <anonymous>:4:293) 
    at callback (http://localhost:63342/Proba/bower_components/angular/angular.js:23481:17) 
    at Scope.$eval (http://localhost:63342/Proba/bower_components/angular/angular.js:15922:28) 
    at Scope.$apply (http://localhost:63342/Proba/bower_components/angular/angular.js:16022:25) 
    at HTMLButtonElement.<anonymous> (http://localhost:63342/Proba/bower_components/angular/angular.js:23486:23) 
    at HTMLButtonElement.eventHandler (http://localhost:63342/Proba/bower_components/angular/angular.js:3296:21)(anonymous function) @ angular.js:12450(anonymous function) @ angular.js:9237Scope.$apply @ angular.js:16027(anonymous function) @ angular.js:23486eventHandler @ angular.js:3296 
angular.js:12450 TypeError: Cannot read property '0' of undefined 
    at new ModalInstanceController (modal-instance-controller.js:12) 
    at invoke (angular.js:4476) 
    at Object.instantiate (angular.js:4484) 
    at angular.js:9142 
    at resolveSuccess (ui-bootstrap-tpls.js:2983) 
    at processQueue (angular.js:14678) 
    at angular.js:14694 
    at Scope.$eval (angular.js:15922) 
    at Scope.$digest (angular.js:15733) 
    at Scope.$apply (angular.js:16030) 
+0

$ scope.modalController不具有財產「結果」,$ scope.modalController.modalInstance有一個屬性「結果」。這就是你得到這個錯誤的原因。 – o4ohel

+0

tnx man第一個錯誤消失了,但是第二個錯誤:TypeError:不能讀取屬性'0'undefined仍然存在... – Bukic

回答

1

這是因爲您正在執行「$ scope.modalController.result ...」,它不存在。 你需要做「$ scope.modalController.modalInstance.result.then ...」

此外,我不知道你爲什麼介紹「modalController」範圍變量?這不是必要的,只是增加了混淆。

+0

tnx man第一個錯誤消失了,但第二個錯誤:TypeError:不能讀取屬性'0'undefined仍然存在... – Bukic

+0

嘿,你知道這個模式可以拖動嗎? – Bukic

+0

我沒有這方面的需求,但有一些例子在這裏......這裏是一個:http://embed.plnkr.co/8CHoiN/preview – o4ohel

0

我有一個模式類似的問題。我認爲這是一個CSS問題,甚至與z-index有關。我想知道是否有一些來自引導庫的CSS屬性隱藏父元素。

0

所以一般情況下,模式不會打開的原因很多。像o4ohel說,失蹤的財產「結果」。那麼你需要給一個尺寸。 我喜歡評論重要的事情,並嘗試打開一個空白的模式,說「你好」。一旦你到達它,你走得更遠

0

好吧,所以我發現了錯誤。第一個是: $ scope.modalController.result ==> $ scope.modalController.modalInstance.result 第二個是: item:$ scope.items [0] item:$ scope.modalInstanceController.items [0]。

正如o4ohel說,也許我不應該使用VAR modalController ....

相關問題