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?

2
  • 4
    You have 2 ng-app tags, Angular cannot support more than one application in a page. Commented Jun 22, 2016 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. Also, see this related SO question Commented Jun 22, 2016 at 19:05

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.