从html访问$ scope?

Him*_*ors 13 angularjs

由于第三方的愚蠢原因,我需要从html访问$ scope.

这就是我正在尝试的:

<html ng-app>
    <!-- head goes here-->
    <body>
        <!--Body goes here-->
        <script type="text/javascript">
            console.log($scope);
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

sub*_*ero 15

因为angular全局暴露,您可以使用:

var scope = angular.element().scope()
Run Code Online (Sandbox Code Playgroud)

例如,如果您在标记中有这个

<div ng-controller="someCtrl" id="someId">{{test}}</div>
Run Code Online (Sandbox Code Playgroud)

您可以someCtrl像这样访问控制器的隔离范围:

var scope = angular.element($("#someId")).scope()
scope.test = "Hello, world!";
Run Code Online (Sandbox Code Playgroud)

(你可能也希望$应用范围,见这里)

  • 我设法访问此元素'<div ng-controller ="Invite"id ="invite"class ="invite">'上的angular.element,但scope.scope不存在. (2认同)