简体   繁体   中英

how to generate list dynamically in angular.js

can you please tell me how to create list dynamically in angulat.js..Actullly I am able to make list when user press add button and fill the field . In other words ,Please check this fiddle whenever you fill the fields it generate a row.And you can get Id when you click the row .Fiddle http://jsfiddle.net/wc4Jm/6/ Now I am trying to do this using bootstrap model .in other words on button click first I show a pop up screen then there is "add" button .on click that it generate the row.but I am getting "undefined".My I insert the model div inside the controller ? here is http://jsbin.com/vubojoxo/4/

Why I am getting this error ? XMLHttpRequest cannot load file:///C:/Users/asd/Desktop/angular/angularproject/dialog.html. Received an invalid response. Origin 'null' is therefore not allowed access.

I am getting this error when I used plunker..and run in my desktop .. I make this 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 code:

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');

    };

});

Check this Plunker

In this example i used angular-ui library which wraps bootstrap's modal to angular based on this StackOverflow Answer

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');
  };

HTML

 <body>

    <div ng-controller="ModalDemoCtrl">


      <div inner-html-bind="" inner-html="modal_html_template" class="hidden">
        <div class="modal-header">
          <h3>I'm a modal!</h3>
        </div>

        <div class="modal-body">

          <div class="form-group">
            <label>Name</label>

            <!-- using $parent because ui-bootstrap nested 2 controllers. this is a workaround -->
            <input type="text" class="form-control" ng-model="$parent.name" placeholder="Enter Name">
          </div>

          <div class="form-group">
            <label>Content</label>

            <!-- using $parent because ui-bootstrap nested 2 controllers. this is a workaround -->
            <input type="text" class="form-control" ng-model="$parent.content" placeholder="Enter Content">
          </div>

        </div>

        <div class="modal-footer">
          <button class="btn btn-primary" ng-click="ok()">OK</button>
          <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
      </div>

      <div class="container">
        <h2>Modal Example <a href="https://stackoverflow.com/questions/24988561">https://stackoverflow.com/questions/24988561</a></h2>
        <button class="btn" ng-click="open()">Open Modal</button>


        <div>

          <ul>
            <li ng-repeat="item in items">
              <a ng-click="getId(item)">{{ item.id }} | {{ item.name + ' ' + item.content  }}</a>
            </li>
          </ul>

        </div>
      </div>

    </div>
  </body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM