目前,我知道如何從文本框中收集輸入并在$scope變數中使用它。但是如果我不想使用$scope變數怎么辦?如果我只是想將輸入作為引數傳遞給函式怎么辦?
目前:
我有一些看起來像這樣的 JSP:
<textarea
required="true"
spellcheck="on"
data-ng-model="editTools.productDescription"
rows="4" >
</textarea>
我在 JSP 中還有一個按鈕,用于呼叫 Angular JS 中的函式:
<button id="description-submit-button"
data-ng-click="mySpecialFunction()"
buttonType="${'primary'}"
htmlButtonType="${'button'}">
<text>"Submit"</text>
</button>
所以,我明白這是有效的。我正在為其分配一個值$scope.editTools.productDescription,然后mySpecialFunction()使用該值。
但是我如何在不使用$scope變數的情況下完成同樣的事情?我希望能夠說類似的話data-ng-click="mySpecialFunction({the stuff the user typed in the text box})"
這可能嗎?如何?
uj5u.com熱心網友回復:
您實際上不必在控制器中宣告模型變數。您可以將您的更改textarea為這個,以便模型是myVariable(您可以隨意命名):
<textarea
required="true"
spellcheck="on"
data-ng-model="myVariable"
rows="4">
</textarea>
然后你可以像這樣myVariable直接傳入mySpecialFunctionHTML 中的呼叫:
<button
id="description-submit-button"
data-ng-click="mySpecialFunction(myVariable)"
buttonType="${'primary'}"
htmlButtonType="${'button'}"
>
<text>"Submit"</text>
</button>
現在在您的控制器中,您的函式將如下所示:
$scope.mySpecialFunction = function (value) {
// value === myVariable
// do something with it here
};
但需要注意的是,angularjs 實際上確實會添加myVariable到$scope幕后,所以如果您真的想要,您仍然可以訪問$scope.myVariable控制器中的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/350141.html
標籤:javascript html angularjs jsp
上一篇:numpy按范圍陣列獲取元素
