AngularJS Bootstrap


Das bevorzugte Stylesheet von AngularJS ist Twitter Bootstrap. Twitter Bootstrap ist derzeit das beliebteste Frontend-Framework.

Bootstrap-Tutorial ansehen。


Bootstrap

Sie können Ihrer AngularJS-Anwendung Twitter Bootstrap hinzufügen. Sie können den folgenden Code in Ihr <head>-Element einfügen:

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

Wenn sich die Website in China befindet, wird empfohlen, Bootstrap aus dem statischen Ressourcen-Repository von Baidu zu verwenden. Der Code lautet wie folgt:

<link rel="stylesheet" href="//apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css">

Das Folgende ist ein vollständiges HTML-Beispiel, das AngularJS-Direktiven und Bootstrap-Klassen verwendet.


HTML-Code

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<body ng-app="myApp" ng-controller="userCtrl">

<div class="container">

<h3>Benutzer</h3>

<table class="table table-striped">
  <thead><tr>
    <th>Bearbeiten</th>
    <th>Vorname</th>
    <th>Nachname</th>
  </tr></thead>
  <tbody><tr ng-repeat="user in users">
    <td>
      <button class="btn" ng-click="editUser(user.id)">
      <span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;Bearbeiten
      </button>
    </td>
    <td>{{ user.fName }}</td>
    <td>{{ user.lName }}</td>
  </tr></tbody>
</table>

<hr>
<button class="btn btn-success" ng-click="editUser('new')">
  <span class="glyphicon glyphicon-user"></span> Neuen Benutzer erstellen
</button>
<hr>

<h3 ng-show="edit">Neuen Benutzer erstellen:</h3>
<h3 ng-hide="edit">Benutzer bearbeiten:</h3>

<form class="form-horizontal">
<div class="form-group">
  <label class="col-sm-2 control-label">Vorname:</label>
  <div class="col-sm-10">
    <input type="text" ng-model="fName" ng-disabled="!edit" placeholder="Vorname">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label">Nachname:</label>
  <div class="col-sm-10">
    <input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Nachname">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label">Passwort:</label>
  <div class="col-sm-10">
    <input type="password" ng-model="passw1" placeholder="Passwort">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label">Wiederholen:</label>
  <div class="col-sm-10">
    <input type="password" ng-model="passw2" placeholder="Passwort wiederholen">
  </div>
</div>
</form>

<hr>
<button class="btn btn-success" ng-disabled="error || incomplete">
  <span class="glyphicon glyphicon-save"></span> Änderungen speichern
</button>
</div>

<script src = "myUsers.js"></script>
</body>
</html>

Ausprobieren »


Directive Explanation

AngularJS-Direktiven Beschreibung
<html ng-app Definiert eine Anwendung (unbenannt) für das <html>-Element
<body ng-controller Definiert einen Controller für das <body>-Element
<tr ng-repeat Durchläuft das Benutzer-Objektarray und platziert jedes Benutzerobjekt in einem <tr>-Element.
<button ng-click Ruft die Funktion editUser() auf, wenn auf das <button>-Element geklickt wird
<h3 ng-show Wenn edit = true, wird das <h3>-Element angezeigt
<h3 ng-hide Wenn edit = true, wird das <h3>-Element ausgeblendet
<input ng-model Verbindet das <input>-Element mit der Anwendung
<button ng-disabled Wenn ein Fehler auftritt oder incomplete = true ist, wird das <button>-Element deaktiviert


Bootstrap Class Explanation

Element Bootstrap-Klassen Definition
<div> container Inhaltscontainer
<table> table Tabelle
<table> table-striped Tabelle mit gestreiftem Hintergrund
<button> btn Schaltfläche
<button> btn-success Erfolgs-Schaltfläche
<span> glyphicon Glyphicon
<span> glyphicon-pencil Stift-Symbol
<span> glyphicon-user Benutzer-Symbol
<span> glyphicon-save Speichern-Symbol
<form> form-horizontal Horizontales Formular
<div> form-group Formulergruppe
<label> control-label Steuerelement-Beschriftung
<label> col-sm-2 Erstreckt sich über 2 Spalten
<div> col-sm-10 Erstreckt sich über 10 Spalten


JavaScript-Code

myUsers.js

angular.module('myApp', []).controller('userCtrl', function($scope) {
$scope.fName = '';
$scope.lName = '';
$scope.passw1 = '';
$scope.passw2 = '';
$scope.users = [
{id:1, fName:'Hege', lName:"Pege" },
{id:2, fName:'Kim', lName:"Pim" },
{id:3, fName:'Sal', lName:"Smith" },
{id:4, fName:'Jack', lName:"Jones" },
{id:5, fName:'John', lName:"Doe" },
{id:6, fName:'Peter',lName:"Pan" }
];
$scope.edit = true;
$scope.error = false;
$scope.incomplete = false;

$scope.editUser = function(id) {
  if (id == 'new') {
    $scope.edit = true;
    $scope.incomplete = true;
    $scope.fName = '';
    $scope.lName = '';
    } else {
    $scope.edit = false;
    $scope.fName = $scope.users[id-1].fName;
    $scope.lName = $scope.users[id-1].lName;
  }
};

$scope.$watch('passw1',function() {$scope.test();});
$scope.$watch('passw2',function() {$scope.test();});
$scope.$watch('fName', function() {$scope.test();});
$scope.$watch('lName', function() {$scope.test();});

$scope.test = function() {
  if ($scope.passw1 !== $scope.passw2) {
    $scope.error = true;
    } else {
    $scope.error = false;
  }
  $scope.incomplete = false;
  if ($scope.edit && (!$scope.fName.length ||
  !$scope.lName.length ||
  !$scope.passw1.length || !$scope.passw2.length)) {
     $scope.incomplete = true;
  }
};

});


Erklärung des JavaScript-Codes

Scope-Eigenschaften Zweck
$scope.fName Modellvariable (Benutzername)
$scope.lName Modellvariable (Nachname des Benutzers)
$scope.passw1 Modellvariable (Benutzerpasswort 1)
$scope.passw2 Modellvariable (Benutzerpasswort 2)
$scope.users Modellvariable (Array von Benutzern)
$scope.edit Wird auf true gesetzt, wenn der Benutzer auf Benutzer erstellen klickt.
$scope.error Wenn passw1 nicht gleich passw2 ist, wird auf true gesetzt.
$scope.incomplete Wenn ein Feld leer ist (length = 0), wird auf true gesetzt.
$scope.editUser Modellvariablen setzen
$scope.watch Modellvariablen überwachen
$scope.test Validieren Sie die Modellvariablen auf Gültigkeit und Vollständigkeit.
Weitere Erweiterungen