5

Is there a way in AngularJs to replace a string?

I'm trying to do something like:

{{string.replace('some', 'thing')}}

Thanks!

2 Answers 2

12

your snippet works!

demo: http://plnkr.co/edit/yNuNeE5yO3rgKAYfGx48?p=preview

html

<body ng-app="app">
  <div>
    <div class="container" ng-controller="mainCtrl">
      <p>
      {{ name.replace('some', 'thing') }}
      </p>
    </div>
  </div>

</body>

js

var app = angular.module('app', []);


app.controller('mainCtrl',function($scope) {

    $scope.name = 'this is some';

  }
);

the output is this is thing

enter image description here

demo: http://plnkr.co/edit/yNuNeE5yO3rgKAYfGx48?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Thanks. I tried that before and, I can't remember why, but I didn't think it worked. I searched through Google and I couldn't find anything. After reading your post, I tried it again, and it worked like a charm! Thanks so much!
2

Why don't you just replace part of the string inside your controller?

So in your view you have: {{myString}}

and in your controller you have: $scope.myString.replace('some', 'thing');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.