从HTML访问$范围是什么? [英] Access $scope from html?

查看:118
本文介绍了从HTML访问$范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于愚蠢的第三方的原因,我需要从HTML访问$范围。

这就是我尝试:

 < HTML NG-应用>
    <! - 头放在这里 - >
    <身体GT;
        <! - 正文到这里 - >
        <脚本类型=文/ JavaScript的>
            的console.log($范围内);
        < / SCRIPT>
    < /身体GT;
< / HTML>


解决方案

由于在全球范围内曝光,你可以使用:

  VAR范围= angular.element()。范围()

例如,如果你在你的标记有这种

 < D​​IV NG控制器=someCtrlID =someId> {{}测试}< / DIV>

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

  VAR范围= angular.element($(#someId))。范围()
scope.test =你好,世界!;

(您可能要$适用范围上,请这里

Because of stupid third-party reasons, I need to access $scope from the html.

This is what I'm attempting:

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

解决方案

Because angular is exposed globally, you can use:

var scope = angular.element().scope()

For example if you have this in your markup

<div ng-controller="someCtrl" id="someId">{{test}}</div>

You can access the isolated scope of the controller someCtrl like this:

var scope = angular.element($("#someId")).scope()
scope.test = "Hello, world!";

(you might want to $apply the scope as well, see here)

这篇关于从HTML访问$范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆