How to add ng-model functionality to a component

后端 未结 2 1299
野趣味
野趣味 2020-12-04 00:48

Angular ng-change on ng-model passed into child directive

Basically, I want to be able to pass in ng-model from a parent directive to a child directive. I could ju

相关标签:
2条回答
  • 2020-12-04 01:30

    Almost the same as @georgeawg answer, but using directive method (since component was introduced in 1.5+, but author has 1.4.8):

    angular.module('myApp', [])
    .controller('MyCtrl', ['$scope', function MyCtrl($scope) {
    
    }])
    .directive('inputComponent', [function () {
            var myDirective = {
                restrict: 'E',
                require: 'ngModel',
                templateUrl: "checkboxComponent.html",
                link : function(scope, element, attrs, ngModelCtrl){
                    scope.updateModel = function(ngModel) {
                        ngModelCtrl.$setViewValue(ngModel);
                    }
                }
            }
            return myDirective;
    }]);
    fieldset {
      margin-top: 1em;
    }
    <script src="//code.angularjs.org/1.4.8/angular.js"></script>
    
    <div ng-app="myApp">
      <div ng-controller="MyCtrl">
        
        <input-component ng-model="value"
                         ng-change="value2=value"></input-component>
                         
                                            
        <fieldset>
          <p>value = {{value}}</p>
          <p>value2 = {{value2}}</p>
        </fieldset>
        
      </div>
      
      <script type="text/ng-template" id="checkboxComponent.html">
        <div>
             <input type="text" ng-model="ngModel" ng-change="updateModel(ngModel)" />
        </div>
      </script>
      
    </div>

    0 讨论(0)
  • 2020-12-04 01:47

    How to add ng-model functionality to a component

    Use one-way < input for input and use the ngModelController API for output:

    app.component("checkboxComponent", {
        bindings: { ngModel: '<' },
        require: { ngModelCtrl: 'ngModel' },
        template: `
              <input type=checkbox ng-model='$ctrl.ngModel'
                     ng-change="$ctrl.ngModelChange()" />
        `,
        controller: function() {
          this.ngModelChange = () => {
            this.ngModelCtrl.$setViewValue(this.ngModel);
          };
        }
    })
    

    The component uses one-way binding for input, and $setViewValue for output. With this method, the ng-change works, and the component can be used as a form element:

    <form name="form1">
         <checkbox-component name="input1" ng-model="vm.formData.input1"
                             ng-change="vm.inp1Change()">
         </checkbox-component>
    </form>
    

    For more information, see

    • AngularJS ngModelController API Reference
    • AngularJS Developer Guide -Implementing custom form controls (using ngModel)

    The DEMO

    angular.module("app",[])
    
    .component("checkboxComponent", {
        bindings: { ngModel: '<' },
        require: { ngModelCtrl: 'ngModel' },
        template: `
            <fieldset>
              <h3>Checkbox Component</h3>
              <input type=checkbox ng-model='$ctrl.ngModel'
                     ng-change="$ctrl.ngModelChange()" />
                     Checkbox
            </fieldset>
        `,
        controller: function() {
          this.ngModelChange = () => {
            this.ngModelCtrl.$setViewValue(this.ngModel);
          };
        }
    })
    <script src="//unpkg.com/angular/angular.js"></script>
      <body ng-app="app">
        <checkbox-component ng-model="value"
                            ng-change="value2=value"> 
        </checkbox-component>
        <fieldset>
          <p>value = {{value}}</p>
          <p>value2 = {{value2}}</p>
        </fieldset>
      </body>

    0 讨论(0)
提交回复
热议问题