0

How does AngularJs make its selectors In HTML without the need/using script tag? I've seen this happening in html via angular allot:

 {{'Something...'}}

How is this possible without the script tag?

4
  • It's not possible, the script should have been included somewhere else Commented Sep 24, 2014 at 21:48
  • 2
    taken to a very VERY basic level, consider the following: template contains your html that has {{foo}}, angular does template.replace("{{foo}}", "bar"). Angular of course does a lot more than that, such as actually parsing the content inside of {{}} to see if it needs to perform any additional logic. Commented Sep 24, 2014 at 21:49
  • Correct me if I'm wrong but it replaced the curly brackets? Commented Sep 24, 2014 at 21:51
  • 2
    yes. The curly brackets aren't getting executed as javascript, instead, angular is reading it, executing functions based on the content of it, and then replacing it with the result. Commented Sep 24, 2014 at 21:52

2 Answers 2

2

Think of it as a templating system. Your html is the template, and the controller is the... controller that you defined in your angular js.

The {{something}} things in your html are usually references to properties on the scope of the controller.

$scope.something = "Hello World!";

Angular simply replaces the {{something}} entries in your html with actual content, for example, with theHtml.replace("{{something}}", $scope.something). Angular can also interpret basic javascript within the brackets so you can do things like <p>{{something + ' Today\'s date is: ' + new Date()}}</p> and actually get the result to output on the page instead of the brackets.

This only explains at a very basic level how the content within the brackets gets replaced with actual content, explaining much more such as how data binding is done will become too much for a single answer on SO.

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

Comments

1

After AngularJs is loaded, it will scan your html document. For every directive or angular mark-up it applies an watcher which sends notifications when that value has changed and by receiving this notification, angular knows UI must be updated asap.

So, for Angular, {{}} it's something that will be verified at a certain time. ( like a directive )

You can read more about this here .

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.