码迷,mamicode.com
首页 > 其他好文 > 详细

Angular 从DOM中获取scope

时间:2016-09-08 14:21:13      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

节选官方文档:

原文:https://docs.angularjs.org/guide/scope

Retrieving Scopes from the DOM.

Scopes are attached to the DOM as $scope data property, and can be retrieved for debugging purposes. (It is unlikely that one would need to retrieve scopes in this way inside the application.) The location where the root scope is attached to the DOM is defined by the location of ng-app directive. Typically ng-app is placed on the <html> element, but it can be placed on other elements as well, if, for example, only a portion of the view needs to be controlled by Angular.

To examine the scope in the debugger:

  1. Right click on the element of interest in your browser and select ‘inspect element‘. You should see the browser debugger with the element you clicked on highlighted.

  2. The debugger allows you to access the currently selected element in the console as $0 variable.

  3. To retrieve the associated scope in console execute: angular.element($0).scope() or just type $scope

 

scope是附加在DOM上,使用了ng-app指令的DOM就是root scope。一般是<html ng-app="app">。但不绝对。

在chrome的调试面板中可以查看某个DOM附加的scope信息。

1. 右键元素,选择检查

2. 在console面板中输入$0并回车,可以显示当前所选的元素。

3. 获取附加在他上麦吗的scope信息。执行angular.element($0).scope() 或 仅输入 $scope

技术分享

Angular 从DOM中获取scope

标签:

原文地址:http://www.cnblogs.com/mafeifan/p/5852640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!