Send data from an angular textarea

4

I have a controller in angular which is responsible by means of a textarea load a content url if it is, if it is not a url does not load any data, the problem is that when wanting to write only text I send indefinite data.

function PostCtrl($scope,$http, $compile, $rootScope, PostServices, CONFIG) {
  $scope.renderHTML = function(html_code)         {
            console.log(html_code);
            return $sce.trustAsHtml(html_code);
        };

  $scope.patternUrl = (function() {
      var regexp = /(https?:\/\/[^\s]+)/gi;
      return {
        test: function(value) {
          //return regexp.test(value);
          if (regexp.test(value)) {

            $scope.form = {
                    name: name,
                }


              return  $scope.$watch('form', function(newVal, oldVal){
                  $http({
                     url: CONFIG.APIURL + 'post/ajaxcheck',
                     method: "POST",
                     data: {'query':newVal}
                   }).success(function (data, status, headers, config) {
                       var publisher = $('.timeline');
                       $scope.postsDetails=data.data;

                     }).error(function (data, status, headers, config) {
                        //ERROR


                   });
          }, true);
          }
        }
      };
    })();

    $scope.send = function () {

      var link = $scope.postsDetails;

    console.log($scope.form.name); //Aqui deberia de imprimir solo el texto si no hay contenido cargado
        $http.post(CONFIG.APIURL + 'post/ajaxpublish', JSON.stringify({
            name: $scope.postsDetails,
            message:   $scope.form.name,
        })).success(function(data, status) {
            $scope.postsList=data.data;
        })


    }


}

HTML

<div class="mdl-card__title loader">
              <div class="post-text-area f-right">
                <textarea class="post-textarea mdl-textfield__input js_publisher_timeline" ng-model="form.name" ng-pattern="patternUrl" name="Url"></textarea>
              </div>
              <div class="f-left mdl-card__menu">
                <img class="_bth img" src="" height="40" width="40">
              </div>

            </div>
            <div class="publisher-timeline" id="pub">
              <ng-responsive-posts data="postsDetails"></ng-responsive-posts>
            </div>

I edit my question and solve my problem:

    function PostCtrl($scope,$http, $compile, $rootScope, PostServices, CONFIG) {     

      //Valido si es una url
      function validateUrl(formText) {
        var regexp = /(https?:\/\/[^\s]+)/gi;
        return regexp.test(formText) ? true : false
      }

      $scope.$watch('post.formText', function(newValue, oldValue) {
        $scope.reqs = [];
        //Si da true paso por aqui
        if (validateUrl(newValue)) {
          $http({
             url: CONFIG.APIURL + 'post/ajaxcheck',
             method: "POST",
             data: {'query':newValue}
           }).success(function (data, status, headers, config) {
                $scope.postsAttach = data.data;
                $scope.post.formText = data.data.title;
             }).error(function (data, status, headers, config) {
                //ERROR


           });
        }else {
//Si doy false paso solo a enviar texto
          $scope.Text = newValue;
        }
      }, true);

//Una vez cargado el contenido texto o url lo envio para ser guardado en el //backend con L5
      $scope.send = function () {

        $http({
           url: CONFIG.APIURL + 'post/ajaxpublish',
           method: "POST",
           dataType: 'json',
           data: JSON.stringify({'name': $scope.postsAttach,'message': $scope.Text})
         }).success(function (data, status, headers, config) {
            $scope.postsAttach = "";
            $scope.Text = "";
            $scope.post.formText ="";
           }).error(function (data, status, headers, config) {
              //ERROR


         });


      }

    }
    
asked by vdjkelly 05.05.2016 в 20:31
source

1 answer

1

Solution (directly from the OP)

function PostCtrl($scope,$http, $compile, $rootScope, PostServices, CONFIG) {     

      //Valido si es una url
      function validateUrl(formText) {
        var regexp = /(https?:\/\/[^\s]+)/gi;
        return regexp.test(formText) ? true : false
      }

      $scope.$watch('post.formText', function(newValue, oldValue) {
        $scope.reqs = [];
        //Si da true paso por aqui
        if (validateUrl(newValue)) {
          $http({
             url: CONFIG.APIURL + 'post/ajaxcheck',
             method: "POST",
             data: {'query':newValue}
           }).success(function (data, status, headers, config) {
                $scope.postsAttach = data.data;
                $scope.post.formText = data.data.title;
             }).error(function (data, status, headers, config) {
                //ERROR


           });
        }else {
//Si doy false paso solo a enviar texto
          $scope.Text = newValue;
        }
      }, true);

//Una vez cargado el contenido texto o url lo envio para ser guardado en el //backend con L5
      $scope.send = function () {

        $http({
           url: CONFIG.APIURL + 'post/ajaxpublish',
           method: "POST",
           dataType: 'json',
           data: JSON.stringify({'name': $scope.postsAttach,'message': $scope.Text})
         }).success(function (data, status, headers, config) {
            $scope.postsAttach = "";
            $scope.Text = "";
            $scope.post.formText ="";
           }).error(function (data, status, headers, config) {
              //ERROR


         });


      }

    }
    
answered by 07.07.2016 / 14:50
source