如何在angular.js中动态生成列表

how to generate list dynamically in angular.js

本文关键字:动态 列表 js angular      更新时间:2023-09-26

你能告诉我如何在angulat.js中动态创建列表吗?实际上,当用户按下添加按钮并填充字段时,我能够制作列表。换句话说,每当您填写字段时,请检查此小提琴,它会生成一行。点击行就能得到Id。fiddle http://jsfiddle.net/wc4Jm/6/现在我试图做到这一点,使用引导模型。换句话说,在按钮点击首先我显示一个弹出屏幕,然后有"添加"按钮。点击,它生成行。但我得到了"未定义"。我是否在控制器中插入模型div ?这是http://jsbin.com/vubojoxo/4/

为什么我得到这个错误?XMLHttpRequest无法加载文件:///C:/Users/asd/Desktop/angular/angularproject/dialog.html收到无效响应。因此,不允许访问原点'null'。

当我使用plunker..并在我的桌面运行时,我得到这个错误。这是html ?

<!doctype html>
<html ng-app="plunker">
<head>
    <script src="angular.js"></script>
    <script src="ui-bootstrap-tpls-0.2.0.js"></script>
    <link href="bootstrap-combined.min.css" rel="stylesheet">
    <script src="index.js"></script>
</head>
<body>
<div ng-controller="DialogDemoCtrl">
    <a class="btn" data-toggle="modal" href="" ng-click="openPopupScreen()">Add Contend</a>
</div>
</body>
</html>

…Dialog.html

<div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h1>Add Element</h1>
</div>
<div class="modal-body">
    <form >
        <label>Name:</label><input type="text" class="span3" ng-model="activeItem.name"></br>
        <label>Content Name:</label><input type="password" class="span3" ng-model="activeItem.content"></br>
        <button type="submit" class="btn btn-success" ng-click="addItem()">Add In List</button>
        <button type="reset" class="btn ">Clear</button>
    </form>
</div>
<div class="modal-footer">
    <a class="btn" data-dismiss="modal" aria-hidden="true">close</a>
</div>

js代码:

var myApp = angular.module('plunker', ['ui.bootstrap']);
myApp.controller('DialogDemoCtrl',  function($scope,$dialog) {
    $scope.items = [];
    $scope.activeItem = {
        id:'',
        name: '',
        content: ''
    };
    $scope.addItem = function () {
        $scope.activeItem.id = $scope.items.length + 1;
        $scope.items.push($scope.activeItem);
        $scope.activeItem = {}; /* reset active item*/
    };
    $scope.getId = function (item) {
        alert('ID: '+item.id);
    };
    $scope.openPopupScreen = function () {
        alert('Check Open pop up screen');
        $dialog.dialog({}).open('dialog.html');
    };
});

检查这个活塞

在这个例子中,我使用了angular-ui库,它将bootstrap的模态包装到angular基于此StackOverflow答案

ModalDemoCtrl

  $scope.items = [];
  $scope.getId = function(item) {
    alert('ID: ' + item.id);
  };
  // This opens a Bootstrap modal
  $scope.open = function() {
    var modalInstance = $modal.open({
      template: $scope.modal_html_template,
      controller: ModalInstanceCtrl
    });
    modalInstance.result.then(function(newItem) {
      // On modal success
      newItem.id = $scope.items.length + 1;
      $scope.items.push(newItem);
    }, function() {
      // On modal cancelation
    });
  }

ModalInstanceCtrl

  $scope.name = '';
  $scope.content = '';
  $scope.ok = function() {
    var response = {
      'name': $scope.name,
      'content': $scope.content
    };
    $modalInstance.close(response);
  };
  $scope.cancel = function() {
    $modalInstance.dismiss('cancel');
  };