0

我是Angular JS的新手。我今天開始學習Angular JS。在我使用jQuery和Bootstrap作爲前端之前。現在我想使用AngularJS顯示Bootstrap對話框。我發現這個,https://angular-ui.github.io/bootstrap/Angular js中的引導對話框

但是,初學者難以理解的東西太多了。我如何糾正我的代碼以顯示引導對話框?

這是我的代碼

<html> 
<head> 
    <title>Angular</title> 
    <link rel="stylesheet" href="http://localhost:8888/angular/bootstrap.css"></link> 
    <script src="http://localhost:8888/angular/angular-js.min.js"></script> 
    <script src="http://localhost:8888/angular/angular-js.animate.min.js"></script> 
    <script src="http://localhost:8888/angular/angular-js.sanitize.min.js"></script> 
    <script src="http://localhost:8888/angular/ui-bootstrap-tpls.min.js"></script> 

</head> 
<body> 
    <div ng-controller="ModalDemoCtrl as $ctrl"> 
     <script type="text/ng-template" id="myModalContent.html"> 
      <div class="modal-header"> 
       <h3 class="modal-title" id="modal-title">I'm a modal!</h3> 
      </div> 
      <div class="modal-body" id="modal-body"> 
       <ul> 
        <li ng-repeat="item in $ctrl.items"> 
         <a href="#" ng-click="$event.preventDefault(); $ctrl.selected.item = item">{{ item }}</a> 
        </li> 
       </ul> 
       Selected: <b>{{ $ctrl.selected.item }}</b> 
      </div> 
      <div class="modal-footer"> 
       <button class="btn btn-primary" type="button" ng-click="$ctrl.ok()">OK</button> 
       <button class="btn btn-warning" type="button" ng-click="$ctrl.cancel()">Cancel</button> 
      </div> 
     </script> 

     <button type="button" class="btn btn-default" ng-click="$ctrl.open()">Open me!</button> 
     <button type="button" class="btn btn-default" ng-click="$ctrl.open('lg')">Large modal</button> 
     <button type="button" class="btn btn-default" ng-click="$ctrl.open('sm')">Small modal</button> 
     <button type="button" class="btn btn-default" ng-click="$ctrl.toggleAnimation()">Toggle Animation ({{ $ctrl.animationsEnabled }})</button> 
     <button type="button" class="btn btn-default" ng-click="$ctrl.openComponentModal()">Open a component modal!</button> 
     <div ng-show="$ctrl.selected">Selection from a modal: {{ $ctrl.selected }}</div> 
    </div> 
</body> 
<script> 
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($uibModal, $log) { 
    var $ctrl = this; 
    $ctrl.items = ['item1', 'item2', 'item3']; 

    $ctrl.animationsEnabled = true; 

    $ctrl.open = function (size) { 
    var modalInstance = $uibModal.open({ 
     animation: $ctrl.animationsEnabled, 
     ariaLabelledBy: 'modal-title', 
     ariaDescribedBy: 'modal-body', 
     templateUrl: 'myModalContent.html', 
     controller: 'ModalInstanceCtrl', 
     controllerAs: '$ctrl', 
     size: size, 
     resolve: { 
     items: function() { 
      return $ctrl.items; 
     } 
     } 
    }); 

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

    $ctrl.openComponentModal = function() { 
    var modalInstance = $uibModal.open({ 
     animation: $ctrl.animationsEnabled, 
     component: 'modalComponent', 
     resolve: { 
     items: function() { 
      return $ctrl.items; 
     } 
     } 
    }); 

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

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

// Please note that $uibModalInstance represents a modal window (instance) dependency. 
// It is not the same as the $uibModal service used above. 

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance, items) { 
    var $ctrl = this; 
    $ctrl.items = items; 
    $ctrl.selected = { 
    item: $ctrl.items[0] 
    }; 

    $ctrl.ok = function() { 
    $uibModalInstance.close($ctrl.selected.item); 
    }; 

    $ctrl.cancel = function() { 
    $uibModalInstance.dismiss('cancel'); 
    }; 
}); 

// Please note that the close and dismiss bindings are from $uibModalInstance. 

angular.module('ui.bootstrap.demo').component('modalComponent', { 
    templateUrl: 'myModalContent.html', 
    bindings: { 
    resolve: '<', 
    close: '&', 
    dismiss: '&' 
    }, 
    controller: function() { 
    var $ctrl = this; 

    $ctrl.$onInit = function() { 
     $ctrl.items = $ctrl.resolve.items; 
     $ctrl.selected = { 
     item: $ctrl.items[0] 
     }; 
    }; 

    $ctrl.ok = function() { 
     $ctrl.close({$value: $ctrl.selected.item}); 
    }; 

    $ctrl.cancel = function() { 
     $ctrl.dismiss({$value: 'cancel'}); 
    }; 
    } 
}); 
</script> 
</html> 

但我的代碼是行不通的。有太多選項我不知道在這個例如「myModalContent.html」是從哪裏來的?你會一步一步解釋在AngularJS中顯示Bootstrap對話框嗎?

我想這樣也

<html> 
<head> 
    <title>Angular</title> 
    <script src="angular-js.min.js"></script> 
    <script src="angular-js.animate.min.js"></script> 
    <script src="angular-js.sanitize.min.js"></script> 
    <script src="angular-js.touch.min.js"></script> 
    <script src="bootstrap.ui.js"></script> 
    <link href="bootstrap.css" rel="stylesheet"> 

</head> 
<body ng-app="MyApp" ng-controller="MyCtrl"> 

    <button class="btn" ng-click="open()">Open Modal</button> 

    <div modal="showModal" close="cancel()"> 
    <div class="modal-header"> 
     <h4>Modal Dialog</h4> 
    </div> 
    <div class="modal-body"> 
     <p>Example paragraph with some text.</p> 
    </div> 
    </div> 

</body> 
<script> 
var app = angular.module("MyApp",["ui.bootstrap.modal"]); 

app.controller('MyCtrl',function($scope){ 
    $scope.open = function() { 
    $scope.showModal = true; 
    }; 
}) 
</script> 
</html> 

它這樣表示:

enter image description here

它不工作爲好。我如何在AngularJS中顯示引導程序對話框?

+0

你有一些語法錯誤。 'ng-controller =「myCtrl as $ ctrl」'你得到了'as'關鍵字的兩倍。此外,我認爲使用美元符號作爲控制器別名是無效的。改用'myCtrl as vm'代替。 – cleftheris

+0

我更新了問題。請看一看。代碼仍然不起作用。 ( –

+0

)你是否檢查過他們的工作樣本?https://plnkr.co/edit/VoLvFSg5d70Zq64w6UI3?p = preview – cleftheris

回答

1

雖然在ng-click中引用了一個函數,但您沒有ctrl.open函數。

也許你可以試試這個嗎?

$ctrl.open = function (size) { 
       var modalInstance = $uibModal.open({ 
        animation: $ctrl.animationsEnabled, 
        ariaLabelledBy: 'modal-title', 
        ariaDescribedBy: 'modal-body', 
        templateUrl: 'myModalContent.html', 
        controller: 'ModalInstanceCtrl', 
        controllerAs: '$ctrl', 
        size: size, 
        resolve: { 
         items: function() { 
          return $ctrl.items; 
         } 
        } 
       }); 
+0

我更新了這個問題,但不僅僅是在工作 –

+0

你得到了什麼錯誤? – defaultcheckbox

1

open()方法返回一個模態實例。這是實際在您的模板上創建模態的內容。

templateurl是您的模板的路徑。在這裏,它尋找myModalContent.html在你的控制器代碼相同的路徑。您需要創建和設計模板,並在您的視圖中調用此模式時加載該模板。

template可以在這裏使用,而不是templateurl對於內聯HTML,如果你沒有足夠的HTML。 「足夠」對你的理解是主觀的。

controller是您的控制器的名稱。你可以隨意定義它。

controllerAs是您的控制器的別名。你可以將它定義爲你喜歡的東西。 $ctrl是默認情況下控制器的別名。如果你從這裏刪除controllerAs選項,你仍然可以在你的模板中使用$ ctrl。儘管如此,使用此功能需要您提供controller選件。

animationsenabled是一個布爾值,可用於通過切換animation屬性在視圖中切換模態動畫。默認爲false

resolve有助於在打開的模式上創建$resolve對象。這有助於從您的items提供所有已解決的值。它基本上是Angular的$routeprovider服務的配置。爲了幫助理解$resolve更好,你可以看看this

既然你是新的角度,就像我,一個偉大的地方開始將理解角度Services。它將成爲您最常用的功能之一。

此外,你可以檢查出plunker上述代碼,編輯它並理解功能。我也是第一次嘗試它。

另外,將angular-ui作爲標籤添加到您的問題中,這樣angular-ui可以介入。

希望這有助於某種方式。