[英]How do I pass the contents of a textbox into angular js as an input parameter, rather than a $scope variable?
Currently, I know how to collect the input from a textbox and use it in a $scope
variable.目前,我知道如何从文本框中收集输入并在
$scope
变量中使用它。 But what if I don't want to use a $scope
variable?但是如果我不想使用
$scope
变量怎么办? What if I simply want to pass the input in as a parameter to a function?如果我只是想将输入作为参数传递给函数怎么办?
CURRENTLY:目前:
I have some JSP that looks like this:我有一些看起来像这样的 JSP:
<textarea
required="true"
spellcheck="on"
data-ng-model="editTools.productDescription"
rows="4" >
</textarea>
I also have a button in JSP that calls a function in Angular JS:我在 JSP 中还有一个按钮,用于调用 Angular JS 中的函数:
<button id="description-submit-button"
data-ng-click="mySpecialFunction()"
buttonType="${'primary'}"
htmlButtonType="${'button'}">
<text>"Submit"</text>
</button>
So, I understand that this works.所以,我明白这是有效的。 I am assigning a value to
$scope.editTools.productDescription
and then mySpecialFunction()
uses that value.我正在为
$scope.editTools.productDescription
分配一个值,然后mySpecialFunction()
使用该值。
But how would I accomplish the same thing without using a $scope
variable?但是我如何在不使用
$scope
变量的情况下完成同样的事情? I'd like to be able to say something like data-ng-click="mySpecialFunction({the stuff the user typed in the text box})"
我希望能够说出类似
data-ng-click="mySpecialFunction({the stuff the user typed in the text box})"
Is this possible?这可能吗? How?
如何?
You don't actually have to declare your model variable in the controller.您实际上不必在控制器中声明模型变量。 You can change your
textarea
to this so that the model is myVariable
(you can name it whatever you want):您可以将
textarea
更改为这样,以便模型为myVariable
(您可以随意命名):
<textarea
required="true"
spellcheck="on"
data-ng-model="myVariable"
rows="4">
</textarea>
Then you can pass myVariable
directly into the mySpecialFunction
call in the HTML like this:然后,您可以将
myVariable
直接传递到 HTML 中的mySpecialFunction
调用中,如下所示:
<button
id="description-submit-button"
data-ng-click="mySpecialFunction(myVariable)"
buttonType="${'primary'}"
htmlButtonType="${'button'}"
>
<text>"Submit"</text>
</button>
Now in your controller your function would look something like this:现在在您的控制器中,您的函数将如下所示:
$scope.mySpecialFunction = function (value) {
// value === myVariable
// do something with it here
};
But just to note, that angularjs does in fact add myVariable
to the $scope
behind the scenes, so if you really wanted to you could still access the value with $scope.myVariable
in your controller.但需要注意的是,angularjs 实际上确实在幕后将
myVariable
添加到$scope
,因此如果您真的想要,您仍然可以在控制器中使用$scope.myVariable
访问该值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.