我如何获得的输入值转换角度的范围$? [英] How do I get an input value into Angular's $scope?

查看:189
本文介绍了我如何获得的输入值转换角度的范围$?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我新的角度,我试图做的很基本的东西。这里是一个视图的一部分(全角文件添加其他地方):

I'm new to Angular, and I am trying to do something really basic. Here is a part of a view (all angular files are added elsewhere) :

 <div ng-controller='ctrl'>
    <input type='text' ng-model='id'>
 </div>

这是我的控制器:

And here is my controller :

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

    // get the scope value here
}]);

我想要做的是非常简单的。我想使用的输入值。我想是这样 $ scope.data = [] $ scope.data.push($ scope.id)创建具有范围的值的数组,但没有奏效。当我试图显示值,我得到了在控制台上的未定义。

What I'm trying to do is really simple. I'd like to use the input value. I tried something like $scope.data = [] and $scope.data.push($scope.id) to create an array with the scope's value, but it didn't work. When I tried to display the value, I got an 'undefined' on the console.

难道你们有什么想法?

编辑:该视图还具有与触发控制器的功能,我试图让我值 NG-点击指令按钮

Edit : The view also has a button with the ng-click directive which trigger the controller's function where I try to get my value.

推荐答案

该值将自动被添加到 $范围,但你必须输入一些东西到输入元素。

The value will automatically be added to the $scope, but you have to type something into the input element.

这就是说,你需要触发的东西来获得这个值。例如,如果你有你的评论 //这里得到的范围值 - 这是只要控制器初始化并再也没有所谓的触发;所以,这是怎么回事,当时登录不确定。但是,如果你把它设置为一个按钮的点击,或什么的,你会看到它的可用:

That said, you need to trigger something to get this value. For example, where you have your comment // get the scope value here -- this is triggered as soon as the controller is initialized and never called again; so, it's going to log undefined at that time. However, if you set it to a button click, or something, you will see it's available:

<div ng-controller='ctrl'>
   <input type='text' ng-model='id'>
   <button ng-click="logId()">Log ID</button>
</div>

和控制器:

module.controller('ctrl',['$scope', function ($scope) {
    $scope.logId = function() {
        console.log($scope.id);
    }
}]);

现在键入内容到输入,然后点击按钮。

Now type something into the input and click the button.

这篇关于我如何获得的输入值转换角度的范围$?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆