Nurdin
Nurdin

Reputation: 23883

Angular - Break line text

I can't save break line text into my database, how to solve this issue?

My save data suppose to be like this

I want to ask something.

Can I?

Not like this

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

Upvotes: 1

Views: 194

Answers (1)

GregL
GregL

Reputation: 38121

Just use the encodeURIComponent() function to encode the newlines correctly into the URL so it will be seen by the server correctly when you submit the GET request.

So your JS becomes:

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

Upvotes: 3

Related Questions