简体   繁体   English

获取Angular中文本字段的值

[英]Get text-field's value in Angular

I have a text-field form in my html in which I wish to extract to my controller. 我的html中有一个文本字段表单,我希望将其提取到控制器中。

Currently it always returns "undefined" 当前,它总是返回“ undefined”

在此处输入图片说明

HTML: HTML:

<center>
    <button ng-click="RequestSubmit()" class="button button-assertive">
    Submit Request!
    </button>
</center>

<label class="item item-input">
<span class="input-label">Remarks</span>
 <input type="text" ng-model="remarkstext">
</label>

Controller: 控制器:

.controller('ctrl', function($http, $scope, dataFactory) {
$scope.RequestSubmit = function($scope){

var t = $scope.remarkstext;
console.log("remarks: "+t);

Removing the "$scope" in the function() does not work. 在function()中删除“ $ scope”不起作用。 What could be the problem? 可能是什么问题呢?

Try this plunker : 试试这个矮人

html: HTML:

<div>
<input type="text" ng-model="remarkstext">
 <button ng-click="RequestSubmit()" class="button button-assertive">
    Submit Request!
    </button>
</div>

JS: JS:

$scope.remarkstext='';
$scope.RequestSubmit = function(){
console.log($scope.remarkstext);
}

USe this code:: 使用此代码::

.controller('ctrl', function($http, $scope, dataFactory) {
    $scope.RequestSubmit = function(){

    var t = $scope.remarkstext;
    console.log("remarks: "+t);

As @weirdpanda said in his comment, you have to remove $scope from the RequestSubmit function. 正如@weirdpanda在他的评论中所说,您必须从RequestSubmit函数中删除$scope Note that this function expects a parameter, and you are calling it without anyone. 请注意,此函数需要一个参数,并且您在没有任何人的情况下调用它。 So in RequestSubmit function, $scope is undefined. 因此,在RequestSubmit函数中, $scope是未定义的。

About the definition of your controller, it is not bad, but take into account that if you minify your code it won't work, you should define like this: 关于控制器的定义,这还不错,但是要考虑到,如果您缩小了代码的使用范围,则应该这样定义:

.controller('ctrl', ['$http', '$scope', 'dataFactory', 'StoreService', function($http, $scope, dataFactory, StoreService) {
    ...
}]);

@jjimenez has mentioned very correctly that you're using a function which expects a parameter without passing it any. @jjimenez非常正确地提到您正在使用一个期望参数而不传递任何参数的函数。 This confuses the internals of AngularJS as it MAY think of that as a special extension function. 这可能会使AngularJS的内部混乱,因为它可能会将其视为特殊的扩展函数。

You have to remove the $scope from the function; 必须从函数中删除$scope furthermore, a very common coding standard is (it actually makes the code a lot more readable and easier to work with): 此外,一个非常通用的编码标准是(它实际上使代码更具可读性并且更易于使用):

.controller( 'ctrl', [ '$scope', '$http', 'dataFactory', function( $scope, 
                                                                   $http, 
                                                                   dataFactory ) {

    $scope.RequestSubmit = function() {
        console.log( "Remarks: " + $scope.remarkstext );
    };

    // ... rest of the logic

}] );

After minification the old code wouldn't have worked because of the fact that when UglifyJS (or equivalent) minifies it, it renames the reserved variables $scope , etc. to e a or something else, which Angular is unable to resolve; 最小化后,旧代码将无法工作,因为当UglifyJS(或等效代码)对其进行最小化时,它会将保留变量$scope等重命名为e a或其他,Angular无法解析; when you use the array notation (I forgot its specific name), angular knows what to inject, only the names have changed. 当您使用array notation (我忘记了它的特定名称)时,angular知道要注入什么,只有名称已更改。

Coming to your problem, one thing can be that you're trying to click the button before filling in the text; 谈到您的问题,一件事可能是您试图在填写文本之前单击按钮。 as you can read one of my answers here , I have explained how the ng-model works. 如您在这里阅读的我的答案之一,我已经解释了ng-model工作原理。 Give that a read, and check! 阅读并检查!

If possible, can you give us a little bit about how you've defined your controller; 如果可以的话,您能否给我们一些有关您如何定义控制器的信息; in the context of HTML. 在HTML上下文中。

if you dont want to use $scope.remarkstext then pass remarkstext in ng-click. 如果您不想使用$ scope.remarkstext,请在ng-click中传递remarkstext。

NOTE : if you want to access model value in controller then you have to use $scope.remarkstext. 注意 :如果要访问控制器中的模型值,则必须使用$ scope.remarkstext。

you are using remarkstext directly so it will give you error undefined. 您直接使用remarkstext ,因此会给您未定义的错误。

use like this if you want value from parameter 如果您想从参数中获取值,请使用像

 <center> <button ng-click="RequestSubmit(remarkstext)" class="button button-assertive"> Submit Request! </button> </center> <label class="item item-input"> <span class="input-label">Remarks</span> <input type="text" ng-model="remarkstext"> </label> 

 $scope.RequestSubmit = function(remarkstext){ var t = remarkstext; console.log("remarks: "+t); } 


your mistake 你的错误

 $scope.RequestSubmit = function($scope){ var t = $scope.remarkstext; console.log("remarks: "+t); } 

i don't understand why you are using $scope in function parameters you can directly use $scope.remarkstext inside function.and remarkstext is in ng-model so it is available in $scope . 我不明白为什么在函数参数中使用$ scope,您可以直接在function.inner中使用$ scope.remarkstext,而remarkstext在ng-model中,因此可以在$ scope中使用 ng-inspector to see scope tree in console. ng-inspector在控制台中查看作用域树。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM