简体   繁体   English

Angular:如何在没有ng-model的情况下获取输入值

[英]Angular: how to get input value without ng-model

I have form with dynamic inserted input to the DOM (from some other plugin). 我有一个动态插入input到DOM(来自其他一些插件)的form Is there way to read value from this input without ng-model on it? 有没有办法在没有ng-model的情况下从这个输入中读取值?

<form name="myForm" data-my-directive>
    <div class="customPlugin">
        <!-- here input without ng-modeal appears: -->
        [ <input type="text" name="foo" value="bar" /> ]
    </div>
</form>

I look at many examples, but everywhere people writes about ng-model ... :( 我看了很多例子,但到处都有人写关于ng-model ... :(

Use a directive that watches for changes. 使用监视更改的指令。

You can then assign this to your scope, if deemed necessary. 如果认为有必要,您可以将其分配给您的范围。

.directive('watchForChanges', function () {
        return {
            link: function(scope, element, attrs) {
                element.on('change', function (e) {
                  console.log(e.target.value);
                  // scope.myValue = e.target.value;
                })
            }
        }
});

PLNKR: http://plnkr.co/edit/qrj8ZbUya5wE0EylFcGG?p=preview PLNKR: http ://plnkr.co/edit/qrj8ZbUya5wE0EylFcGG?p = preview

You can use a directive: 您可以使用指令:

JSFiddle 的jsfiddle

.directive('myDirective', function() {
    return {
        link: function(scope, element, attrs) {
            console.log(element.find('input').attr('value'));
        }
    };
});

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

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