0

I've the following simple Angular JS trail, which contains the basic CRUD operations:

<html>
<head>
    <title>CRUD</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
</head>
<body>
    <div ng-app>
        Simple Expression Evaluator:<br/>
        <input ng-model="calculator"/><br/>
        {{calculator + "=" + $eval(calculator)}}
    </div>
    <h3>CRUD - Comments</h3>
    <div ng-app="commentapp">
      <ul ng-controller="commentController">
        <li ng-repeat="user in users">
          {{user.name}} wrote "{{user.comment}}"
          <br/><a href="#" ng-click="remove(user)">Delete</a>
          <a href="#" ng-click="edit(user)">Edit</a>
        </li>
        <li>
          <input id="name" ng-model="current.name" value="{{current.name}}" />
          <input id="name" ng-model="current.comment" value="{{current.comment}}" />
        </li>
        <li>
          <button ng-click="save(current)">
            Save
          </button>
          <button ng-click="addNew(current)">
            Add New User
          </button>
        </li>
      </ul>
    </div>
    <script>
        var app = angular.module("commentapp", []);
        app.controller("commentController", function($scope) {
          $scope.users = [{
            "name": "Qwe",
            "comment": "Great!"
          }];
          $scope.current = {};
          $scope.addNew = function(user) {
            $scope.users.push(user);
          };
          $scope.edit = function(user) {
            $scope.current = user;
          };
          $scope.save = function(user) {
            $scope.current = {};
          };
          $scope.remove = function(user) {
            var index = $scope.users.indexOf(user);
            $scope.users.splice(index, 1);
          };
        });
    </script>
</body>
</html>

However the output shows:

enter image description here


So, the expression evaluator works perfectly, which means Angular JS is tied up correctly. But the rest of the components don't work at all. Instead of Qwe, I get the expression {{user.name}}. What am I doing wrong here?

Vineet
  • 897
  • 10
  • 27
  • 4
    You have 2 `ng-app` tags, Angular cannot support more than one application in a page. – Nikos Paraskevopoulos Jun 22 '16 at 19:02
  • 2
    @Vineet if you want more than one appliaction in a page, you should bootstrap module manually with `angular.bootstrap`. With single app your code works: [Plunker](https://plnkr.co/edit/CyE2KIgLkOtedW56KLhV?p=preview). Also, see [this related SO question](http://stackoverflow.com/questions/18571301/angularjs-multiple-ng-app-within-a-page) – Serhii Holinei Jun 22 '16 at 19:05

1 Answers1

1

Hi I tried your script and seem working when I remove the first ng-app

nov the html part is like this

<h3>CRUD - Comments</h3>
    <div ng-app="commentapp">
      <ul ng-controller="commentController">
        <li ng-repeat="user in users">
          {{user.name}} wrote "{{user.comment}}"
          <br/><a href="#" ng-click="remove(user)">Delete</a>
          <a href="#" ng-click="edit(user)">Edit</a>
        </li>
        <li>
          <input id="name" ng-model="current.name" value="{{current.name}}" />
          <input id="name" ng-model="current.comment" value="{{current.comment}}" />
        </li>
        <li>
          <button ng-click="save(current)">
            Save
          </button>
          <button ng-click="addNew(current)">
            Add New User
          </button>
        </li>
      </ul>
    </div>

look at this fiddle

hope this help

rick
  • 1,869
  • 13
  • 22