-1

I'm new to AngularJS, I would like to update my data.json but that I don't know where my file users.json should be and I have this error:

Error: $injector:modulerr http://errors.angularjs.org/1.3.14/$injector/modulerr?p0

Heres my code :

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
    var app = angular.module('myApp', ['ngResource']);

    app.factory('User', ['$resource',
                      function($resource){
                          return $resource('/users/:userId', {userId:'@id'},
                            {
                                'update': {method: 'PUT'}
                            }
                          );
                      }
                    ]);
    app.controller('UsersCtrl', function ($scope, User) {
          $scope.users = User.query();
          $scope.validate=function(user) {
              user.$update();
          }
          $scope.delete=function(user) {
              user.$delete();
          }
          $scope.create = function (newUserName) {
              var user = new User();
              user.name = newUserName;
              user.$save();
              $scope.users.push(user);
          }
      });

</script>
<body ng-app="myApp" ng-controller="UsersCtrl">

        <ul>
              <li ng-repeat="user in users">
                <input type="text" ng-model="user.name" />
                <button ng-click="validate(user)">Valider</button>
                <button ng-click="delete(user)">Supprimer</button>
              </li>
        </ul>
Nouveau utilisateur :
    <input type="text" ng-model="newUserName" />
    <button ng-click="create(newUserName)">Créer</button>

</body>
</html>

Anyone have an idea please?

kguest
  • 3,804
  • 3
  • 29
  • 31
carine
  • 51
  • 1
  • 8

2 Answers2

1

You forgot to add the ngResource file.

<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-resource.min.js"></script>

From ngResource page:

You can download this file from the following places:

  • Google CDN

    e.g. //ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-resource.js

  • Bower

    e.g. bower install angular-resource@X.Y.Z

  • code.angularjs.org

    e.g. "//code.angularjs.org/X.Y.Z/angular-resource.js" where X.Y.Z is the AngularJS version you are running.

Wédney Yuri
  • 1,267
  • 12
  • 21
0

Working Fiddle: http://jsfiddle.net/softvar/rq9eupee/1/

Add angular-resource script below the angular.js file depending upon the version you need within head tag.

<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-resource.min.js"></script>

If you want to read some data from the file users.json, have it on server(Backend) and populate it as per your requirements. If you simply want to show the list of users, fetch it from server, server will read the file and returns it contents. If for some purpose, you need to read the file and display it contents on front-end, have it inside your js/ folder. But assuming, you read it to send the response, it should be on backend.

softvar
  • 17,917
  • 12
  • 55
  • 76
  • Check my updated answer. Feel free to ask your doubts, and please mark it as an answer so that this question won't appear as unanswered to others. – softvar May 30 '15 at 21:24