0

I have a JSON string (jsonString).
I will need to format this string, according my template (myTemplate).

Is there a way to make this via JavaScript in AngularJS? Not in HTML!

var jsonString = {
    comp_code: 100,
    comp_name: 'Test company',
    comp_url: 'Some url',
    comp_note: 'Some notes',
    comp_acc: '1002000'
};

var myTemplate = 'Code: {{comp_code}}, Name: {{comp_name}}, Company account: {{comp_acc}}';

I want a get output string such as this:

'Code: 100, Name: Test company, Company account: 1002000'
2
  • 2
    Your jsonString isn't a JSON string. It's just a JavaScript object. Commented Feb 15, 2017 at 11:55
  • for me looks like a typical case for a directive. but you did not really tell what you want to achieve. Commented Feb 15, 2017 at 11:58

1 Answer 1

3

You can just use Template literals and call variable directly from inside a string.

var jsonString = {
  comp_code: 100,
  comp_name: 'Test company',
  comp_url: 'Some url',
  comp_note: 'Some notes',
  comp_acc: '1002000'
};

var myTemplate = `Code: ${jsonString.comp_code}, Name: ${jsonString.comp_name}, Company account: ${jsonString.comp_acc}`;
console.log(myTemplate)

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

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.