2017-05-23 76 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

不客气;) –