在从 HTML 代码调用的 JavaScript 函数中使用 $scope 变量 - AngularJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14237551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:06:55  来源:igfitidea点击:

Use $scope variable in a JavaScript function called from the HTML code - AngularJS

javascripthtmlfunctionscopeangularjs

提问by Tomarto

I'm using Angular and I'm trying to get $scope to use it in a JavaScript function. Here is an example:

我正在使用 Angular,我试图让 $scope 在 JavaScript 函数中使用它。下面是一个例子:

I have an html tag something like this:

我有一个像这样的 html 标签:

<a id="doSomething" onclick="javascript: doSomething(this);">Do Something</a>
<div ng-show="flag">Click!</div>

and a JavaScript function something like this:

和一个像这样的 JavaScript 函数:

function doSomething(obj) {
    //$scope.flag = true;
    return true;
}

What I want is to use the $scope in this function. I haven't found a way to inject it. The thing is that I must use this way to call the function. Do you have any solution to this? I would appreciate it!

我想要的是在这个函数中使用 $scope 。我还没有找到注入它的方法。问题是我必须使用这种方式来调用函数。你有什么解决办法吗?我会很感激!

Thanks!

谢谢!

回答by Andrew Joslin

If you really have to.. here's how:

如果你真的必须......这里是如何:

HTML:

HTML:

<a onclick="doThing(this);">Do it!</a>

JS:

JS:

function doThing(el) {
  var $scope = angular.element(el).scope();
  $scope.thing = newVal;
  $scope.$apply(); //tell angular to check dirty bindings again
}

Make sure to read the docs for angular.element: http://docs.angularjs.org/api/angular.elementAnd for scope.$apply: http://docs.angularjs.org/api/ng.$rootScope.Scope#$apply

确保阅读 angular.element 的文档:http://docs.angularjs.org/api/angular.element 和 scope.$apply:http: //docs.angularjs.org/api/ng .$rootScope。范围#$应用

What is your use case, though? There might be a better way.

不过,您的用例是什么?可能有更好的方法。

回答by sidonaldson

You can also do it inline:

您也可以内联进行:

JS:

JS:

$scope.doThis = function(str){
     alert(str);
     $scope.$apply();
}

HTML:

HTML:

<button onclick="angular.element(this).scope().doThis('TEST');"></button>

Caveat - scope nesting may affect the results but if the angular code is in the parent controlled this works fine out the box

警告 - 范围嵌套可能会影响结果,但如果角度代码在父控制中,则可以正常工作

回答by Mark Rajcok

This is the same as @Andy's answer, but I mention it in case people want to see how to get the $scope from an event instead of this:

这与@Andy 的答案相同,但我会提到它,以防人们想了解如何从事件中获取 $scope 而不是this

HTML:

HTML:

<a onclick="doThing(event);">Do it!</a>

JS:

JS:

function doThing(ev) {
  var $scope = angular.element(ev.srcElement).scope();          
  $scope.flag = true;
  $scope.$apply(); //tell angular to check dirty bindings again
}

回答by Jigar Patel

You should call angular ng-click for this and then pass scope from there.

您应该为此调用 angular ng-click,然后从那里传递范围。

Here is the example i created for you. http://plnkr.co/edit/UDLJYuZhR4yfiocSPjKJ

这是我为您创建的示例。http://plnkr.co/edit/UDLJYuZhR4yfiocSPjKJ

Mark it as answer if it solves your problem.

如果它解决了您的问题,请将其标记为答案。