4

わかりました、これは Angular の学習に関する今日の 3 部作の最後の質問です。

パート1

パート2

次のエラーが表示されます (ビューでユーザーを選択しようとすると:

TypeError: object is not a function
    at Object.source (http://localhost:3000/assets/angular.js?body=1:6300:13)
    at getMatchesAsync (http://localhost:3000/assets/angular-ui-bootstrap.js?body=1:1820:30)
    at http://localhost:3000/assets/angular-ui-bootstrap.js?body=1:1871:13
    at http://localhost:3000/assets/angular.js?body=1:12030:15
    at Array.forEach (native)
    at forEach (http://localhost:3000/assets/angular.js?body=1:134:11)
    at $setViewValue (http://localhost:3000/assets/angular.js?body=1:12029:5)
    at http://localhost:3000/assets/angular.js?body=1:11423:14
    at Object.Scope.$eval (http://localhost:3000/assets/angular.js?body=1:7994:28)
    at Object.Scope.$apply (http://localhost:3000/assets/angular.js?body=1:8074:23) angular.js?body=1:5688
(anonymous function) angular.js?body=1:5688
(anonymous function) angular.js?body=1:4785
Scope.$apply angular.js?body=1:8076
listener angular.js?body=1:11422
jQuery.event.dispatch jquery.js?body=1:5096
elemData.handle

このコードで(SOの回答に従って更新されます:

  <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>


<script>


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

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


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


</script>

typeahead を使用してサーバーからすべてのユーザーを取得しようとしています。

plunker コードの最新バージョンは こちら

質問: 1. サーバーからコレクション データを取得し、Angular を使用してこのコードを変更して先行入力を実行する方法は?. 2. このエラーは何を意味し、このコードに関連してどのように発生するのか。

4

1 に答える 1

3

問題はindex関数ではなく、typeaheadディレクティブの使用法にあります。これはあなたが望むものです(あなたのリソースのnameプロパティで検索したいと仮定します:User

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

あなたの例が機能するように、ここであなたのプランカーを編集しました:)

于 2013-08-19T15:39:53.537 に答える