Get ng-model from a input inside directive and put it in an attribute of directive

Armin_Fisher

I have "nibTextbox" directive with an input inside it,input has a ng-model,I want that ng-model value always available in "value" attribute of directive.(I don't wanna use replace)

    angular.module('nib', [])
    .directive('nibTextbox', function () {
        return {
            restrict: 'E',
            scope: {
                id: '@',
                title: '@',
            },
            compile: function (element, attributes) {

                var linkFunction = function (scope, element, attributes) {

                }
                return linkFunction;
            },
            controller: ['$scope', '$http', function ($scope, $http) {

            }],
            template: '<div value="{{nibTextBoxValue}}"><img src="" alt=""/><label>{{title}}</label><input id="{{id}}_txt" type="text" ng-model="nibTextBoxValue"></input></div>'
        };
    });


<nib-textbox id="ngArmin1" title="ngArmin1Title" value="{{nibTextBoxValue}}"></nib-textbox>
AranS

value is not valid for a <div> element. So let's change it to data-div. This is how it will look more or less (I usually work with typescript but I'll use plain javascript to pass the idea):

 angular.module('nib', [])
    .directive('nibTextbox', function () {
        return {
            restrict: 'E',
            scope: {
                id: '@',
                title: '@',
            },
            compile: function (element, attributes) {

                var linkFunction = function (scope, element, attributes) {

                }
                return linkFunction;
            },
             // Injected $element for manipulating attributes                
             controller: ['$scope', '$http', '$element', function ($scope, $http, $element) {


              $scope.$watch("nibTextBoxValue", function(newValue) {
              $element.attr("data-value", newValue);
          });
       }],
            templateUrl: 'template.html' // Extracting the template to a file
        };
    });

The directive template (template.html):

<div>
  <img src="" alt=""/><label>{{title}}</label>
  <input id="{{id}}_txt" type="text" ng-model="nibTextBoxValue"></input>
  </div>

In addition, remove the value attribute from your directive:

<nib-textbox id="ngArmin1" title="ngArmin1Title"></nib-textbox>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ng-model for `<input type="file"/>` (with directive DEMO)

Pass ng-model attribute to custom directive

Add attribute directive inside directive in angular

AngularJS Bound Ng-Model Inside Directive Has Wrong Attribute Name

how to get ng-model value in directive

How to get clicked item in directive inside ng-repeat

Attribute directive to work on an element inside another directive

How to modify directive scope attribute only from the inside

Simple custom directive add ng-attribute to input element

Access ng-model from inside directive link function

angularjs Prepopulate input text inside ng-repeat directive

How to change css class attribute from ng directive?

change ng-model value from directive

Getting Attribute value in ng-repeat from a Directive

AngularJS Directive: Get Object from Attribute

get directive attribute defined in HTML in directive controller

Angular: how to get a template ng-model into a directive for filtering

Getting an attribute of a directive from another directive?

Angular Attribute Directive form input

Directive inside ng repeat

How to access ng-model from attribute directive?

Angular 1.5.8 input custom directive ng-model

compile directive inside another directive in ng-if

How to put Input Text and ngMessage inside a Directive templateUrl? Is it possible?

Controller to be put inside Directive

md-checkbox does not get set by ng-model value inside a directive's link() function

AngularJS : ng-model directive

Use directive to change @Input attribute

How to get the value of an input with a type of number in a custom attribute directive