2014-10-07 51 views
1

我不能將斷行文本保存到我的數據庫中,如何解決這個問題?Angular - 斷行文本

我保存假設是這樣

I want to ask something. 

Can I? 

不喜歡這個

I want to ask something. Can I? 

HTML

<textarea name="" cols="" rows="" class="form-control" ng-model="rule.message" required></textarea> 
<button type="submit" class="btn btn-default" ng-click="create()">Save</button> 

JS數據

myControllers.controller('MemberRuleCreateCtrl', ['$scope', '$location', 
    '$http', 
    function($scope, $location, $http) { 
     $scope.rule = {}; 
     $scope.create = function() { 
      $http({ 
       method: 'GET', 
       url: 'http://xxxxx.my/api/create_rule.php?&message=' + $scope.rule.message 
      }). 
      success(function(data, status, headers, config) { 
       alert("Rule successful created"); 
       $location.path('/member/rule'); 
      }). 
      error(function(data, status, headers, config) { 
       alert("No internet connection."); 
      }); 
     } 
    } 
]); 

請指教。謝謝。

+0

這消息應該包含'\ n \ N'字符,如果有換行符 – tymeJV 2014-10-07 23:23:27

回答

3

只需使用encodeURIComponent()函數將新行正確地編碼到URL中,以便在提交GET請求時服務器可以正確看到換行符。

所以,你的JS變爲:

myControllers.controller('MemberRuleCreateCtrl', ['$scope', '$location', 
    '$http', 
    function($scope, $location, $http) { 
     $scope.rule = {}; 
     $scope.create = function() { 
      $http({ 
       method: 'GET', 
       url: 'http://xxxxx.my/api/create_rule.php?&message=' + encodeURIComponent($scope.rule.message) 
      }). 
      success(function(data, status, headers, config) { 
       alert("Rule successful created"); 
       $location.path('/member/rule'); 
      }). 
      error(function(data, status, headers, config) { 
       alert("No internet connection."); 
      }); 
     } 
    } 
]);