简体   繁体   English

如何将文本框的内容作为输入参数而不是 $scope 变量传递给 angular js?

[英]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.

相关问题 Angular / JS:如何将范围变量传递给javascript函数? - Angular/JS: How do I pass a scope variable to a javascript function? 如何将JS变量传递到INPUT语句的“ value =”参数中 - How do I pass a JS variable into the “value=” parameter of an INPUT statement 如何在angular js中保存$ scope变量的副本? - How do I preserve a copy of my $scope variable in angular js? 如何使文本框输入自动完成/自动建议显示文本框内的第一个匹配项,而不是列表? - How do you make a textbox input autcomplete/autosuggest that shows the first match inside the textbox, rather than a list? 如何将范围变量传递给angular中的本机输入min属性? - How to pass a scope variable to a native input min attribute in angular? 如何将 onclick 参数视为变量而不是 Javascript 中的字符串? - How do I treat the onclick parameter as a variable rather then a string in Javascript? 如何将在daterangepicker中选择的日期范围传递给变量或返回到javascript中的输入文本框? - How do I pass the date range selected in the daterangepicker to a variable or back to my input textbox in javascript? 如何将 php/html 中的程序名称作为变量传递给 javascript,而不是在“$.get”语句中对其进行硬编码 - How do I pass a program name from php/html, as a variable to javascript rather than hard coding it in the ' $.get' statement 如何将参数传递给角度服务? - How do I pass parameter to an angular service? 角js中$ scope的可变范围如何工作? - How variable scope of $scope in angular js works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM