2016-09-18 11 views
0

私はかなりシンプルなアプリケーションを書いていますが、私は編集(編集コントローラ)部分のコードに固執しているようです。私は学生のリストを持っています、私は更新のために1つを選択します。しかし、私は "オブジェクトを含むが、配列を持っている期待された応答"というエラーを取得します。単一オブジェクトの代わりに複製オブジェクトの配列を取得する

私が直接Webサービスを照会するとき、私は enter image description here

を取得するが、私は要素を検査し、ネットワーク]タブに移動したときに、私はこの

enter image description here

を参照してくださいここに私のコードです。

var StudentManagement = angular.module('StudentManagement',  ['ngRoute','ngResource','ui.bootstrap']); 

StudentManagement.config(function ($routeProvider) { 
    $routeProvider 
    .when("/", { 
     templateUrl: "list.html", 
     controller: "HomeController" 
    }) 
    .when("/add", { 
     templateUrl: "add.html", 
     controller: "AddController" 
    }) 
    .when("/edit/:editId", { 
     templateUrl: "edit.html", 
     controller: "EditController" 
    }) 
    .otherwise({ 
     redirectTo: "/" 
    }); 


    }); 

    StudentManagement.factory('Student', function ($resource) { 
    return $resource('/api/Student/:id', { id: '@id' }, { update: { method: 'PUT' } }); 
}); 

StudentManagement.controller("HomeController", 
function ($scope, $location, Student) { 

    $scope.search = function() { 
     $scope.students = Student.query(); 
    };// end search 

    $scope.reset = function() 
    { 
     $scope.search(); 
    }// end reset function 

    $scope.search(); 


    }); 

    StudentManagement.controller("EditController", 
    function ($scope, $location, $routeParams, Student) { 
    // get the student given the specific id 
    var id = $routeParams.editId; 
    $scope.student=Student.get({id: id}); 

    $scope.updateForm = function() { 
     Student.update({id:id}, $scope.student, function() { 
      $location.path('/'); 
     }); 
    }// end edit function 

    $scope.cancelForm = function() { 
     $location.path('/'); 
    }// end cancel function 


}); 
+0

現在のコードが実行可能なようにしたい場合は、あなたがあなたのAPIのArrayを返却する場合、あなたがでIsArray使用する必要があるサーバー

からオブジェクトのオブジェクトを返すことができます。サービスメソッドでtrueです。 – nikhil

+0

それは配列を返すべきではないので、私はブラウザから直接Apiを呼び出すとき、[{"id":5、 "firstname": "somaya"、 "lastname": "bliss"、 "age ":20、" studentId ":" 1A40000000 "}]。アレイがフロントエンドで作成されている理由は何ですか? – nahaelem

+0

その配列...角括弧は配列を表しています。あなたは明らかにサーバーから配列を返しています。 – nikhil

答えて

0

サーバーからオブジェクトの配列を戻しています。

したがってリソース定義でisArray : trueを追加する必要があります。

$resource('/api/Student/:id', { id: '@id' }, 
        { update: { method: 'PUT',isArray : true} 
      }); 

それとも

あなたは

+0

角度はあなたの追加が好きではないようです。コンパイルは失敗します。 – nahaelem

+0

oppps、問題を修正しました、それは動作オブジェクトである必要があります –

+0

私は自分の問題を理解したと思います。それが動作する場合あなたに戻ってきます – nahaelem

関連する問題