使用输入文本以角度POST

时间:2015-09-08 14:08:11

标签: javascript jquery angularjs

所以我需要添加列表,但我不能使用按钮和表单。 (不能因为我使用角度制作单页应用程序)

所以我有按钮+当我按它时出现输入文字。所以我想在输入列表名称后点击(创建新列表)并点击ENTER,我想在之后再次隐藏输入。

问题

如何对我输入新列表名称并按下ENTER的角度说?我想要的东西是:我按+,出现输入,我输入列表名称,按回车键,我的角度控制器启动工作(输入的POST数据),输入文本隐藏。

我的代码

我的+“按钮”:

  <a  href="javascript:hideshow(document.getElementById('adiv'))"> <div class="category theme_border_6" style="position: relative; left: 0px; float: left; margin: 10px 10px 10px 10px;  top: 0px;"><div class="name theme_title_color_3"> + </div>

<input type="text" ng-model="listname" id="adiv" ng-keypress="runScript(e)" class="form-control" style="font:24px bold; display: none" placeholder="NEW LIST" />

<div class="count theme_color_4_light"></div>
</div></a>

我的脚本显示输入文字:

function hideshow(which){

    if (which.style.display=="none"){
        which.style.display="block";
    which.focus();}

    else
    which.focus();
    $(which).blur(function () {
    which.style.display = "none"
    });
}

我对于帖子的角度(runScript无法正常工作,但下面的代码很好,我只需要以某种方式说出我在输入文本字段中输入的角度):

myApp.controller('createListController', ['$scope', '$log', '$http','$location',
    function($scope, $log, $http, $location ){

      $scope.runScript = function (e) {
          if (e.keyCode == 13) {
              var list = {
                  name: $scope.listname

              };
              console.log(e);
              $http({
                  method: 'POST',
                  url: 'http://localhost/anydocopy/public/lists',
                  data: list
              })
                  .success(function () {
                      console.log('true');
                      return $location.path('');
                  })
                  .error(function () {
                      console.log('false');
                  });
          }};
    }]);

1 个答案:

答案 0 :(得分:1)

问题是您没有在$ scope上定义的 runScript 函数中接收事件对象。使用ng-keypress =“runScript($ event)”更改ng-keypress =“runScript(e)”。

<!-- Example: --> 
<input type="text" ng-model="listname" ng-keypress="runScript($event)" .../>