2016-05-12 24 views
0

我試圖從cordova s​​qlite中獲取數據並將其添加到範圍中。並綁定視圖頁面中的數據。但我得到錯誤「無法讀取未定義的推送」,並且沒有數據顯示在視圖頁面中。

//我控制器

$scope.codesubmit=function(data){ 
     var query = "SELECT * FROM item_master WHERE menucode = ?"; 
     $cordovaSQLite.execute(db, query, [data.menucode]).then(function (res) { 
     alert("inside success"); 
$scope.items.push({"menucode":"+res.rows.item(0).menucode+","menuname":"+res.rows.item(0).menuname+","quantity":"+data.quantity+","price":"+res.rows.item(0).price+"}); 
      alert(items); 
    },function (err) { 
       console.error(err); 
      }); 

//我的看法頁面

<tbody id=Tabledata> 
      <tr ng-repeat="item in items"> 
      <td>{{item.menucode}}</td> 
      <td>{{item.menuname}}</td> 
      <td>{{data.quantity}}</td> 
      <td>{{item.price}}</td> 
      <td>{{total}}</td> 
      <td><a class='delete' href='#' ng-click()id='"+res.rows.item(0).id+"'>Delete</a></td> 
      </tr> 
     </tbody> 

回答

1

您需要在開始使用它之前定義$ scope.items數組。

$scope.codesubmit=function(data){ 
$scope.items = []; //define the array here 
    var query = "SELECT * FROM item_master WHERE menucode = ?"; 
    $cordovaSQLite.execute(db, query, [data.menucode]).then(function (res) { 
    alert("inside success"); 
$scope.items.push({menucode: res.rows.item(0).menucode, menuname: res.rows.item(0).menuname,quantity: data.quantity, price: res.rows.item(0).price}); 
     alert(items); 
},function (err) { 
      console.error(err); 
     }); 

在你的函數定義的開始添加$scope.items = [];將創建一個空數組,你可以通過從數據庫中檢索到的數據填充。你也可以在全球定義它,如果你想在別的地方訪問它。

+0

感謝您的答案。我知道它但「」menucode「:」+ res.rows.item(0).menucode +「」這行返回「」+ res.rows.item(0).menucode +「」。如何獲得值,即菜單代碼。 – Ranendra

+0

將您的代碼'res.rows.item(0).menucode'放在雙引號之外。裏面的所有內容都被視爲字符串。另外我不確定爲什麼你甚至需要'+'符號。我已經相應地編輯了我的答案。試試看。高興地幫助:) –

+0

感謝您的幫助..它像魅力工作.. – Ranendra

0

似乎$ scope.items是不確定的。首先定義您的$ scope.items對象。

$scope.items = []; 
+0

在push之前添加此行將在每次push之前清空數組,因此該數組將只包含當前元素 –

+0

其實我只想說在過程之前定義$ scope.items對象。是的,如果他可以將此行添加爲全局變量或類似的東西,那會更好。 – oguzhan00