问题描述
我想看看是否有通过外部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:
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访问范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!