Problem description
When learning angular, when the sample code is written to load the picture, it is recommended to use one-way data binding symbols [] to bind the path of the picture, and then the double braces are written incorrectly.
try
First of all, according to the recommended writing method in the book:
<img [src]="imgUrl" />
The code that defines variables in the controller is omitted here.
Then try it in the wrong way in the book:
<img src="{{ imgUrl }}" />
The final test results found that both writing methods can load the picture normally, and there was no problem of parsing {{ imgUrl }} into a string.
Reread
I found that the expected results were not achieved, so I read again what the book said:
If the browser loads this template before Angular runs, it will try to load the image with the string {{ imgUrl }} as Url, which of course will get a "404 Not Found" error. Before Angular runs, the browser displays a broken image on the page.
After carefully reading the original text above, I found that there are conditions for errors to occur. Then when will the above error be triggered?
guess
Recalling the double curly braces in angularjs, similar to angular, there was a problem that variables were not loaded normally when used, resulting in the phenomenon that the page directly displays {{...}}. This problem occurred at that time, either slowly loading or repeated refreshes. The reason is that the template has been loaded, but angularjs is not fully loaded.
So, I boldly guessed that when angular loading slowly, there will be problems with the writing of {{}}.
(The author has tried several times here, but there is no problem. If anyone tries, please correct me.)
The difference between two bindings
The difference between using [] and {{}} is not very big. Both are a one-way binding implementation method in angular, but it is not just using the form of {{}}. After parsing the expression in brackets, the result will be converted into a string. And [] will not be converted into a string.
Summarize
Some problems may not be explained well at our current level, but it is still easy to make a convincing guess. Even if this guess is proven to be wrong one day in the future, it will only be the beginning of your new accumulation.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.