clestcruz
clestcruz

Reputation: 1111

Insert data-bind values inside textarea as a default canned message using angular

I'm trying to figure out how I can insert data-bind values inside a textarea as a canned message. Here is an example

<textarea ng-init='message="Buy {{ get_value }} and get {{ free_value }} free for every purchase"' class="text-input referral-message" ng-model="message" rows="5">
</textarea>

I tried this kind of solution, but the output shows the data-bind and not the value itself


$timeout(function() {

    ReferralService.settings().$promise.then(function(settings) {
        $scope.purchase_count = settings.credits;
    });

    $scope.buildMessage = function(val){
      return "Buy " + val + " and get 1 free for every purchase"
    }
}, 1);

Upvotes: 1

Views: 73

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

I would try to avoid such approach of ng-init

If you want to use ng-init, I would try something like:

<textarea ng-init="message=buildMessage(get_value,free_value)" 
          ng-model="message" rows="5">
</textarea>

Where buildMessage is:

$scope.buildMessage = function(val,free){
  return "Buy " + val + " and get " + free + " free for every purchase"
}

I think this is a clean way and doesn't enter noise to your HTML.

Try to stay with simple HTML as possible.


[EDIT]

$timeout(function() {

  ReferralService.settings().$promise.then(function(settings) {
    $scope.purchase_count = settings.credits;
  });
});

$scope.buildMessage = function(val){
  return "Buy " + val + " and get 1 free for every purchase"
}

Upvotes: 2

Related Questions