如何将angularjs值分配给jquery [英] How to assign angularjs value to jquery
本文介绍了如何将angularjs值分配给jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通过 {{myvalue}}
来在视图中打印值。
作为
$ scope.myvalue =123;
我想将此值 myvalue
发送给ajax调用。所以我想将这个angularjs值赋值给jquery值。
我如何将这个angularjs值赋给jquery值?
我试过这样的
< script>
var newvalue = {{myvalue}};
< / script>
但是我得到 SyntaxError:意外的标记{
我该怎么做?
解决方案
您可以获取元素的范围。
<脚本>
var scope = angular.element($(body))。scope();
var newvalue = scope.myvalue
< / script>
示例 -
<!DOCTYPE html>
< html ng-app =plunker>
< head>
< meta charset =utf-8/>
< title> AngularJS Plunker< / title>
< script> document.write('< base href =''+ document.location +'/>');< / script>
< link rel =stylesheethref =style.css/>
< script src =app.js>< / script>
< script>
function test(){
var scope = angular.element($(body))。scope();
var newvalue = scope.name;
alert(newvalue);
}
< / script>
< / head>
< body ng-controller =MainCtrl>
< p> Hello {{name}}!< / p>
< / body>
< / html>
.js
var app = angular.module('plunker',[]);
app.controller('MainCtrl',function($ scope){
$ scope.name ='World';
test()
});
I am printing a value in a view by {{ myvalue }}
.
I am assigning that value from the controller as
$scope.myvalue = "123";
I want to send this value myvalue
to ajax call.. So i want to assign this angularjs value to jquery value.
How can i assign this angularjs value to the jquery value ?
I tried like this
<script>
var newvalue = {{myvalue}};
</script>
But i am getting SyntaxError: Unexpected token {
How can i do this ?
解决方案
you can get the scope of element .
<script>
var scope = angular.element($("body")).scope();
var newvalue = scope.myvalue
</script>
Example--
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
<script data-require="jquery" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="app.js"></script>
<script>
function test(){
var scope = angular.element($("body")).scope();
var newvalue = scope.name;
alert(newvalue);
}
</script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
</body>
</html>
.js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
test()
});
这篇关于如何将angularjs值分配给jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文