简体   繁体   English

如何在javascript中访问Angularjs $ scope?

[英]How to access Angularjs $scope in javascript?

Here's the fiddle http://jsfiddle.net/altermind/yak10smq/1/ 这是小提琴http://jsfiddle.net/altermind/yak10smq/1/

I need to declare variable 'appetite' in the controller 'EntrynewCtrl', then pass it to the javascript function, something like that: 我需要在控制器“ EntrynewCtrl”中声明变量“ appetite”,然后将其传递给javascript函数,如下所示:

  <script>
    var a = appetite;
    </script>

When I do "console.dir(scope);" 当我做“ console.dir(范围);” I see in console: 我在控制台中看到:

$$childScopeClass

I can allocate that variable via console, but have no idea how to access it in the script. 我可以通过控制台分配该变量,但不知道如何在脚本中访问它。

UPD: gave wrong fiddle, now it's correct UPD:提供了错误的提琴,现在是正确的

Please try this, 请尝试一下

angular.element('#YOUR-ELEMENT-SELECTOR').scope().$apply(function(scope){
  scope.doStuff();
});

Another way, 其他方式,

angular.element('.ng-scope').each(function(e, t){  
    console.log(t,angular.element(t).scope());
});

you may need this tool AngularJS Batarang read this blog for more detail Debugging AngularJS Apps from the Console 您可能需要此工具AngularJS Batarang阅读此博客以了解更多详细信息从控制台调试AngularJS应用

 angular.element(targetNode).scope()

-> ChildScope {$id: "005", this: ChildScope, $$listeners: Object, $$listenerCount: Object, $parent: Scope…}

angular.element(targetNode).isolateScope()

-> Scope {$id: "009", $$childTail: ChildScope, $$childHead: ChildScope, $$prevSibling: ChildScope, $$nextSibling: Scope…}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM