从发送jQuery的数据AngularJS的控制器 [英] Sending data from jQuery to controller of AngularJS
问题描述
我想传递的jQuery生成到AngularJS的控制器的数据。是否有任何可能的方式来做到这一点。
< textarea的NG-点击=showSelectedText(selection.text)NAME =editor1ID =editor1COLS =118行=35>
jQuery来收集数据:
$(#editor1)。选择(函数(){ VAR的选择= getSelected()
如果(选择)
{ 警报(选择);
} }); 功能getSelected(){
如果(window.getSelection){
返回window.getSelection();
}
否则,如果(document.getSelection){
返回document.getSelection();
}
其他{
VAR选择= document.selection和放大器;&安培; document.selection.createRange();
如果(selection.text){
返回selection.text;
}
返回false;
}
返回false;
}
AngularJS的控制器:
$ scope.showSelectedText =功能(fromUI){ 警报(文字是:+ fromUI);
};
您需要做出一些改变。就像给一个id到已经定义了你的控制器,如元素
< DIV ID =demoElementNG-应用='MyModule的'NG控制器=myController的>
< / DIV>
在你的控制器保持此功能相同,因为它是。即
$ scope.showSelectedText =功能(fromUI){ 警报(文字是:+ fromUI);
};
从textarea的
现在,你可以删除NG点击=showSelectedText(selection.text),并使用下面一行从你的jQuery code调用角度的功能。
<$p$p><$c$c>angular.element(document.getElementById('demoElement')).scope().showSelectedText(jQueryObjectOfSelectedText);您可以从您的jQuery code你在哪里让你选定的文本,如
调用这个函数 $(#editor1)。选择(函数(){ VAR的选择= getSelected()
如果(选择)
{
。angular.element(的document.getElementById('demoElement'))范围()showSelectedText(选择)。
} }); 功能getSelected(){
如果(window.getSelection){
返回window.getSelection();
}
否则,如果(document.getSelection){
返回document.getSelection();
}
其他{
VAR选择= document.selection和放大器;&安培; document.selection.createRange();
如果(selection.text){
返回selection.text;
}
返回false;
}
返回false;
}
I want to pass data generated by jQuery into controller of AngularJS. Is there any way possible to do this.
<textarea ng-click="showSelectedText(selection.text)" name="editor1" id="editor1" cols="118" rows="35">
jQuery to gather Data:
$( "#editor1" ).select(function() {
var selection = getSelected()
if(selection)
{
alert(selection);
}
});
function getSelected() {
if (window.getSelection) {
return window.getSelection();
}
else if (document.getSelection) {
return document.getSelection();
}
else {
var selection = document.selection && document.selection.createRange();
if (selection.text) {
return selection.text;
}
return false;
}
return false;
}
Controller of AngularJS:
$scope.showSelectedText = function(fromUI) {
alert("Text is : "+ fromUI);
};
You need to make few changes. Like give an id to the element where you've defined your controller, like
<div id="demoElement" ng-app='MyModule' ng-controller="MyController">
</div>
In your controller remain this function same as it is. ie
$scope.showSelectedText = function(fromUI) {
alert("Text is : "+ fromUI);
};
Now you could remove ng-click="showSelectedText(selection.text)" from the textarea and call angular's function from your jquery code by using below line.
angular.element(document.getElementById('demoElement')).scope().showSelectedText(jQueryObjectOfSelectedText);
You could call this function from your jQuery code where you're getting your selected text, like
$( "#editor1" ).select(function() {
var selection = getSelected()
if(selection)
{
angular.element(document.getElementById('demoElement')).scope().showSelectedText(selection);
}
});
function getSelected() {
if (window.getSelection) {
return window.getSelection();
}
else if (document.getSelection) {
return document.getSelection();
}
else {
var selection = document.selection && document.selection.createRange();
if (selection.text) {
return selection.text;
}
return false;
}
return false;
}
这篇关于从发送jQuery的数据AngularJS的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!