2017-05-23 74 views
0

我想使用輸入文本向我的數據庫添加新評論,但我不想使用表單或onclick。這裏是我輸入的文本結構使用輸入按鈕將數據插入數據庫

<input type="text" class="comment_input" ng-model="comment" ng-model-options="{updateOn : 'change blur'}" placeholder="To enter" /> 

當我嘗試使用綁定,它表明我的意見時,我按回車鍵,但我想它插入到我的數據庫API ..這裏的是我在插入數據功能。

$scope.insertData=function(){ 
    console.log($scope.comment); 
    console.log($scope.lang_code); 
    console.log($scope.user_id); 
    console.log($scope.product_id); 
    var dataPromise = Data.setData("API_URL","user_id="+$scope.user_id+"&product_id="+$scope.product_id+"&comment="+$scope.comment+"&lang="+$scope.lang_code); 
    dataPromise.then(function(greeting) { 
     console.log(greeting); 
     if (greeting.data.result == true) { 
      $scope.requestProductDetail(1,5,1); 
      $timeout(function() { 
      window.scrollTo(0,document.body.scrollHeight); 
      }, 0, false); 
     } 
    }, function(reason) { 
    }, function(update) { 
    }); 
}}]); 

有人嗎?

啊,以及如何刷新本頁後我做了什麼? * insertdata /的onclick按鈕

+0

你可以使用NG-按鍵指令HTTPS調用$ window.location.reload()://docs.angularjs。 org/api/ng/directive/ngKeypress – talentedandrew

回答

0

您可以使用此指令觸發進入關鍵事件

app.directive('enterKeyPressed', function() { 
    return function (scope, element, attrs) { 
     element.bind("keydown keypress", function (event) { 
      if(event.which === 13) { 
       scope.$apply(function(){ 
        scope.$eval(attrs.enterKeyPressed); 
       }); 
       event.preventDefault(); 
      } 
     }); 
    }; 
}); 

如果您正在使用NG-重複顯示註釋從HTML中使用它,如下

<input type="text" class="comment_input" ng-model="comment" enter-key-pressed="insertData()" placeholder="To enter" /> 

,只需將新評論推入評論陣列

要重新加載頁面,您可以使用$ route服務的reload方法。注$窗口在控制器中,然後在insertData的end()函數

希望它可以幫助

+0

我已更新我的回答 –

+0

謝謝! 現在有效:D – beginnerprogrammer

+0

不客氣;) –