角度-换行文本

Angular - Break line text

本文关键字:文本 换行 角度      更新时间:2023-09-26

我无法将换行符文本保存到数据库中,如何解决此问题?

我的保存数据应该是这样的

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.");
            });
        }
    }
]);

只需使用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.");
            });
        }
    }
]);