AngularJS Applications

Ita za a kama a kama sheyi a kama a kama.

Make shopping list

A kama a kama tafara shugabanci shopping list, a kama a kama a kama shugabanci a kama shugabanci:

My shopping list
  • {{x}}×

{{errortext}}


Tafara sheyi

Abubu 1: Tawo kanan

A kama a kama sheyi a kama a kama. myShoppingList A kama sheyi a kama a kama. A kama sheyi a kama a kama. myCtrl

A kama sheyi za a kama a kama. products A kama a kama ninku a kama sheyi. kuma aya sheyi a kama ninku. Daga $scope

Daga HTML, a kama a cee ta a cee ta daga ninku. ng-repeat Amara za a cee ta bikiya sheyi kowa, ana za a baya kowa sheyi a bikiya.

Example

Hari hau, a kama a kama HTML listu a kama a cee ta abubu daga ninku.

<script>
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
  $scope.products = ["牛奶", "面包", "奶酪"];
});
</script>
<div ng-app="myShoppingList" ng-controller="myCtrl">
  <ul>
    <li ng-repeat="x in products">{{x}}</li>
  </ul>
</div>

Try It Yourself

Abubu 2: Tawo kanan shugabanci

Daga HTML, tawo kanan tafara, kuma aya sheyi a kama. ng-model Amara za a cee ta bikiya sheyi kowa, ana za a baya kowa sheyi a bikiya.

在控制器中,创建一个名为 addItem kuma aya sheyi a kama. addMe Daga girma zata a kama shugabanci a bikiya. products Daga ninku.

Tawo kanan, kuma aya sheyi a kama. ng-click Amara, amara za a cee ta bikiya sheyi kowa, ana za a baya kowa sheyi a bikiya. addItem 函数。

Example

现在我们可以将商品添加到购物清单中:

<script>
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
  $scope.products = ["牛奶", "面包", "奶酪"];
  $scope.addItem = function () {
    $scope.products.push($scope.addMe);
  }
});
</script>
<div ng-app="myShoppingList" ng-controller="myCtrl">
  <ul>
    <li ng-repeat="x in products">{{x}}</li>
  </ul>
  <input ng-model="addMe">
  <button ng-click="addItem()">Add</button>
</div>

Try It Yourself

第 3 步:删除商品

我们还希望能够从购物清单中删除商品。

在控制器中,创建一个名为 removeItem 的函数,该函数将您想要删除的项目的索引作为参数。

在 HTML 中,为每个项目创建一个 <span> 元素,并为其提供一个 ng-click 指令,该指令使用当前 $index 调用 removeItem 函数。

Example

现在我们可以从购物清单中删除商品:

<script>
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
  $scope.products = ["牛奶", "面包", "奶酪"];
  $scope.addItem = function () {
    $scope.products.push($scope.addMe);
  }
  $scope.removeItem = function (x) {
    $scope.products.splice(x, 1);
  }
});
</script>
<div ng-app="myShoppingList" ng-controller="myCtrl">
  <ul>
    <li ng-repeat="x in products">
      {{x}}<span ng-click="removeItem($index)">✕</span>
    </li>
  </ul>
  <input ng-model="addMe">
  <button ng-click="addItem()">Add</button>
</div>

Try It Yourself

第 4 步:错误处理

该应用有一些错误,例如,如果您尝试两次添加相同的商品,该应用就会崩溃。另外,也不应该允许添加空商品。

我们将通过添加新商品前检查值来解决这个问题。

在 HTML 中,我们将添加一个用于错误消息的容器,并在有人尝试添加现有商品时编写错误消息。

Example

购物清单,可以写入错误消息:

<script>
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
  $scope.products = ["牛奶", "面包", "奶酪"];
  $scope.addItem = function () {
    $scope.errortext = "";
    if (!$scope.addMe) {return;}
    if ($scope.products.indexOf($scope.addMe) == -1) {
      $scope.products.push($scope.addMe);
    } else {
      $scope.errortext = "该商品已经在您的购物清单中。";
    }
  }
  $scope.removeItem = function (x) {
    $scope.errortext = "";
    $scope.products.splice(x, 1);
  }
});
</script>
<div ng-app="myShoppingList" ng-controller="myCtrl">
  <ul>
    <li ng-repeat="x in products">
      {{x}}<span ng-click="removeItem($index)">✕</span>
    </li>
  </ul>
  <input ng-model="addMe">
  <button ng-click="addItem()">Add</button>
  <p>{{errortext}}</p>
</div>

Try It Yourself

Step 5: Design

The application can run, but it can have better design. We use W3.CSS stylesheet to design our application.

Add W3.CSS stylesheet and include appropriate classes throughout the application, the result will be the same as the shopping list at the top of this page.

Example

Use W3.CSS stylesheet to design your application:

<link rel="stylesheet" href="https://www.codew3c.com/lib/style/w3.css">

Try It Yourself