2

I am starting with Angularjs + rails backend and trying to fetch users data from the server - equivalent to to controller/index action in rails.

I have followed several tutorials and found this code as the most clear one....

questions 1. How to properly link angular module into views ? 2. How to fetch data using typeahead and sample data in this post.

here is plunker version of the code

Code is as follows:

views

<div ng-app='users'>
  <div class='container-fluid' ng-controller="UsersIndexCtrl">
    <pre>Model: {{result | json}}</pre>
    <input type="text" ng-model="result" typeahead="suggestion for suggestion in users($viewValue)">
  </div>
</div>

controller

<script>

  // ['ui.bootstrap', 'ngResource'])

 var app = angular.module('users', ['ui.bootstrap', 'ngResource']);


 // factory - resources users
 // equivalent to rails users/index
          app.factory('Users', function($resource) {
              return $resource('/users.json', {}, {
                  index: { method: 'GET', isArray: true}
              });
          });

 // factory - user resource
 // equivalent to users show, update
          app.factory('User', function($resource) {
              return $resource('/users/:user_id.json', {}, {
                  show: { method: 'GET' },
                  update: { method: 'PUT' }
              });
          });

// controller - users/ index
// equivalent to rails controller rails/index

  var UsersIndexCtrl = function($scope, users) {

      $scope.users = users;
  };

</script>

and I am stack here, as I am getting this error:

Error: Unknown provider: usersProvider <- users

The goal of this code is to use typehead and provide data to the user.

My '/users.json' url is as follows:

[{"full_name":"Lia Cartwright","id":1,"image_url":"no-icon.jpg"},{"full_name":"Hilton Turner","id":2,"image_url":"no-icon.jpg"},{"full_name":"Aubrey Barrows","id":3,"image_url":"no-icon.jpg"},{"full_name":"Donnie Kris","id":4,"image_url":"no-icon.jpg"},{"full_name":"Eryn Rath","id":5,"image_url":"no-icon.jpg"},{"full_name":"Caden Fay","id":6,"image_url":"no-icon.jpg"},{"full_name":"Arlie Tromp","id":7,"image_url":"no-icon.jpg"},{"full_name":"Rico Klein","id":8,"image_url":"no-icon.jpg"},{"full_name":"Gudrun Dare","id":9,"image_url":"no-icon.jpg"},{"full_name":"Nathan Langworth","id":10,"image_url":"no-icon.jpg"},{"full_name":"Deanna Stroman","id":11,"image_url":"no-icon.jpg"},{"full_name":"Shania Stroman","id":12,"image_url":"no-icon.jpg"},{"full_name":"Lupe Harvey","id":13,"image_url":"no-icon.jpg"},{"full_name":"Constance Armstrong","id":14,"image_url":"no-icon.jpg"},{"full_name":"Reagan Tremblay","id":15,"image_url":"no-icon.jpg"},{"full_name":"Murray Sipes","id":16,"image_url":"no-icon.jpg"},{"full_name":"Dandre Klocko","id":17,"image_url":"no-icon.jpg"},{"full_name":"Haylee Monahan","id":18,"image_url":"no-icon.jpg"},{"full_name":"Florence Harber","id":19,"image_url":"no-icon.jpg"},{"full_name":"Norberto Hoppe","id":20,"image_url":"no-icon.jpg"}] 
4

1 回答 1

6

You must inject Users not users, it's case sensitive.


Side notes:

No need to create two models, replace:

app.factory('Users', function($resource) {
  return $resource('/users.json', {}, {
    index: { method: 'GET', isArray: true}
  });
});

app.factory('User', function($resource) {
  return $resource('/users/:user_id.json', {}, {
    show: { method: 'GET' },
    update: { method: 'PUT' }
  });
});

with:

app.factory('User', function($resource) {
  return $resource("users/:id", { id: '@id' }, {
    index:   { method: 'GET', isArray: true, responseType: 'json' },
    show:    { method: 'GET', responseType: 'json' },
    update:  { method: 'PUT', responseType: 'json' }
  });
})

Other thing, in your controller, do:

var UsersIndexCtrl = function($scope, User) {
  $scope.users = User.index();
};

Last thing, consider using: Angular Rails resource

于 2013-08-19T11:47:54.223 回答