最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular: how to get input value without ng-model - Stack Overflow

programmeradmin0浏览0评论

I have form with dynamic inserted input to the DOM (from some other plugin). Is there way to read value from this input without ng-model on it?

<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... :(

I have form with dynamic inserted input to the DOM (from some other plugin). Is there way to read value from this input without ng-model on it?

<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... :(

Share Improve this question asked Mar 22, 2016 at 17:22 mwlmwl 1,48814 silver badges19 bronze badges 5
  • Hi can you setup a fiddle with your plugin/extension or even name it to get started? – d.h. Commented Mar 22, 2016 at 17:27
  • Just to get you startet here is a solution using dom manipulation via a controller: stackoverflow./a/31741293/3298029 which adds the ng-model dynamically. – d.h. Commented Mar 22, 2016 at 17:33
  • 1 You can try with angular.element: docs.angularjs/api/ng/function/angular.element – Yosvel Quintero Commented Mar 22, 2016 at 17:33
  • What exactly do you want to do with this value? Because if it is just reading it all you would need is to use jQuery to monitor/read the values of that input. – Jon Commented Mar 22, 2016 at 17:36
  • 1 If you are doing DOM manipulation outside of Angular, you may want to rethink how you are managing your application as you won't be leveraging the application state management features Angular provides. – Chic Commented Mar 22, 2016 at 18:18
Add a ment  | 

2 Answers 2

Reset to default 3

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

You can use a directive:

JSFiddle

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

评论列表(0)

  1. 暂无评论