Rocky Yost
Rocky Yost

Reputation: 150

AngularJs: Replace a part of a string

Is there a way in AngularJs to replace a string?

I'm trying to do something like:

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

Thanks!

Upvotes: 5

Views: 27611

Answers (2)

Jossef Harush Kadouri
Jossef Harush Kadouri

Reputation: 34207

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

Upvotes: 12

Matt Way
Matt Way

Reputation: 33141

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');

Upvotes: 2

Related Questions