从外部js函数AngularJS访问范围 [英] AngularJS access scope from outside js function

查看:137
本文介绍了从外部js函数AngularJS访问范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看是否有通过外部JavaScript函数(完全不相干到目标控制器)

I'm trying to see if there's a simple way to access the internal scope of a controller through an external javascript function (completely irrelevant to the target controller)

我见过一对夫妇的其他问题在这里,

I've seen on a couple of other questions here that

angular.element(#范围),范围();

会检索一个DOM元素的范围,但我的努力正在产生不正确的结果。

would retrieve the scope from a DOM element, but my attempts are currently yielding no proper results.

这里的的jsfiddle: http://jsfiddle.net/sXkjc/5/

Here's the jsfiddle: http://jsfiddle.net/sXkjc/5/

我目前正在经历从纯JS来折角的过渡。我试图做到这一点的主要原因是保持我原来的库code完好尽可能;保存需要去到每个功能添加到控制器。

I'm currently going through a transition from plain JS to Angular. The main reason I'm trying to achieve this is to keep my original library code intact as much as possible; saving the need for me to add each function to the controller.

这是我怎么可能去实现这个任何想法?以上小提琴评论也是欢迎的。

Any ideas on how I could go about achieving this? Comments on the above fiddle are also welcome.

推荐答案

您需要使用 $范围。$适用()如果你想从angularjs像一个jQuery控制之外做出一个范围值的任何更改/ JavaScript的事件处理程序。

You need to use $scope.$apply() if you want to make any changes to a scope value from outside the control of angularjs like a jquery/javascript event handler.

function change() {
    alert("a");
    var scope = angular.element($("#outer")).scope();
    scope.$apply(function(){
        scope.msg = 'Superhero';
    })
}

演示:小提琴

这篇关于从外部js函数AngularJS访问范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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