0

既存の値に関する検証に問題があります。データベースに存在しない場合に、2 つの値を比較して新しい値のみを追加する方法がわかりません。私は試してみましangular.forEach()たが、比較(私が行っていることangular.equals())がfalseではなく、それが間違っている場合は常に新しいオブジェクトが追加されます。オブジェクトは、データベース内で一度だけ存在する必要があります。

ここに私の作成機能があります:

$scope.createItem = function (createItem) {
   //here I have to define a query to compare createItem.lname with the table list (array items from the db) in the view.

   //That was the code:
   angular.forEach(nameslist, function (value) {
     if (angular.equals(createItem.lname, value.lname)) {
           CrudService.create(createItem).then(
              function () {
                //success code
                $scope.createItem = null;
              },
              function (err) {
               //error code
              });
           }
     }
   });
} 

誰か助けてくれませんか..解決方法がわかりません。

4

1 に答える 1

0
var itemAbsent = namelist.map(function(value){
   return value.name;
}).indexOf(createItem.name) < 0;

if (nameAbsent){
   CrudService.create(createItem).then(
          function () {
            //success code
            $scope.createItem = null;
          },
          function (err) {
           //error code
          });
       }
}
于 2015-07-09T15:38:34.263 に答える