JavaScript Replacing part of string in angularjs with json values replacing part of string python,replacing part of string in

I am trying to replace string words which are enclosed in $ with values of JSON.

For example:

I have json.

$scope.data = {
  name: "Some Name",
  user: "User Name",
  designation: "Designation",
  fullName:"User Full Name"
}

And I have String:

$scope.str="User name of $user$ is $name$ and designation is  $designation$";

Is there any angular method from which I can directly replace variables from this string?

I tried using loop through keys and verifying, It is working but there are more than 100 keys in json and there might be less than 20 keys will get used in string.

I am expecting a better approach which improve performance.

Answer:1

1). String.prototype.replace. It actually makes sense to do it in pure Javascript with String.prototype.replace method, it's going to be more effective. For example like this:

$scope = {};
$scope.data = {
    name: "Some Name",
    user: "User Name",
    designation: "Designation",
    fullName: "User Full Name"
};

$scope.str = "User name of $user$ is $name$ and designation is  $designation$";

$scope.str = $scope.str.replace(/\$(\w+)\$/g, function(a, b) {
    return typeof $scope.data[b] !== 'undefined' ? $scope.data[b] : ''; 
});

alert($scope.str);
Answer:2

How to unescape string escaped using StringEscapeUtils in front end. Thank you PS: I use EXT-js3.4 & Jsp.

How to unescape string escaped using StringEscapeUtils in front end. Thank you PS: I use EXT-js3.4 & Jsp.

I have tabs in Bootstrap: <ul class="nav nav-tabs" role="tablist"> <li>tab1</li> <li>tab2</li> <li>tab3</li> <li>tab4</li> <li>...

I have tabs in Bootstrap: <ul class="nav nav-tabs" role="tablist"> <li>tab1</li> <li>tab2</li> <li>tab3</li> <li>tab4</li> <li>...

  1. jquery check bootstrap version
  2. jquery check bootstrap validation
  3. jquery check bootstrap viewport
  4. jquery check if bootstrap modal is open
  5. jquery check if bootstrap tab is active
  6. jquery check if bootstrap modal is closed
  7. jquery check if bootstrap is loaded
  8. jquery check if bootstrap dropdown is open
  9. jquery check if bootstrap modal is visible
  10. jquery check bootstrap checkbox

I am having some difficulties with this problem. I want to render some html elements given a tree structure. For example this javascript tree object: let htmlTree = { id: "a", children: [{ ...

I am having some difficulties with this problem. I want to render some html elements given a tree structure. For example this javascript tree object: let htmlTree = { id: "a", children: [{ ...

For dynamic page changes without having to reload the whole content, I have found this very simple working solution: Tutorial: http://css-tricks.com/rethinking-dynamic-page-replacing-content/ Demo: ...

For dynamic page changes without having to reload the whole content, I have found this very simple working solution: Tutorial: http://css-tricks.com/rethinking-dynamic-page-replacing-content/ Demo: ...