未加星标

AngularJS Tutorial: Introduction to AngularJS Scopes

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
Introduction to AngularJS Scopes

In the previous tutorial of this series we learned how to test controllers using Jasmine and Karma. In this post we discussed how data is passed from controller to view using $scope , which is the glue that links model and view. In this tutorial we’ll talk more about scopes, what are some possible pitfalls in using them, and later we’ll talk about the ways to overcome possible problems, as well as get over Batarang, a Chrome extension from AngularJS team, which can be used to analyze scopes inside an AngularJS application.


By adding the ng-app directive to some HTML tag we specify that an AngularJS will be created and in the process of application creation Angular creates the so-called root scope . Some other directives such as ng-controller create a child scope. To visualize scopes one can use a Google Chrome extension called Batarang . The picture below shows the root scope for the AngularJS application and the child scope created by HelloController, which contains a single variable myModel .


AngularJS Tutorial: Introduction to AngularJS Scopes

What if we created another controller, say to work not with greetings but farewells and that controller would contain a variable with the same name as in HelloController ? It is possible to have variables with the same name that belong to different scopes. The snippet below shows the code for our GoodbyeController . While variables have the same name, the content is different.


Now we should add another section to our index.html file and attach a controller to it as shown below.


It should be noted that it’s necessary to add the link to the controller’s code to the HTML file. The source code for this tutorial can be found here . Now we have two scopes inside the root scope, each of which was created by one of the two controllers, which is depicted in the following images.


AngularJS Tutorial: Introduction to AngularJS Scopes

The myModel variable in the scope of the first controller has the value ‘World’ and the value ‘Everyone’ in the scope of the second controller.


AngularJS Tutorial: Introduction to AngularJS Scopes

When we write tests for our second controller, the code is pretty much the same as in the first case, but things are different with e2e tests because we extracted elements by their model and now we have two models with the same name. To solve this problem one should replace a piece of code that finds a single element


element(by.model ('myModel') )


by the code that extracts the list of elements with the same model name and then specify the position of the desired element in the list.


element.all(by.model('myModel')).get(1)


Nested AngularJS Scopes

A more complex case is shown by the following snippet of code when we have an article nested in a section and each of them has its own controller.


In each of the controllers we have variables with the same name, myModel . The code for the parent controller is shown below.


Also, each controller has a variable specific to it. The following snippet show the code for a child or nested controller.


Now we can use Batarang to explore scopes. We see that the myModel variable in the parent scope has the value different than in the child scope as shown by the screenshots below.


AngularJS Tutorial: Introduction to AngularJS Scopes

Also, we have access to the variable created in the parent scope inside the child scope as can be seen from the web page.


AngularJS Tutorial: Introduction to AngularJS Scopes

The explanation of the fact that we have access to a variable created in the parent scope from inside the child scope is that parent and child scope are linked with javascript Prototypal Inheritance relationship, which means that when the onlyParent variable is not found in the child scope, JavaScript looks for it in the parent, parent of a parent, etc. scopes.


The question is whether we can access the value of the myModel variable defined in the parent scope from the child scope. The answer is yes and this can be accomplished by the snippet of code shown below.


<p>{{$parent.myModel}}</p>


The takeaway from this post is that AngularJS scopes are difficult to work with because AngularJS directives may create their own scopes and variables defined in a parent scope can be hidden by the ones defined in child scopes. This can be error prone but there are some best practices that help one to avoid this kind of problems.


Summary

In this tutorial we talked about scope creation and how to visualize scopes using a Chrome extension called Batarang. Also, we talked about nested scopes and to what problems that could lead as well as how to do end-to-end testing in this case. In the next tutorial we’ll talk about the best practices that allow to avoid problems with AngularJS nested scopes.


Resources

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: AngularJSChromeJavaScriptHTMLJava
分页:12
转载请注明
本文标题:AngularJS Tutorial: Introduction to AngularJS Scopes
本站链接:http://www.codesec.net/view/530089.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(26)