Angular 1.5 component with ng-model

后端 未结 2 829
南旧
南旧 2021-02-11 22:25

Is it possible to use ng-model with a component? I would like to bind a scope variable to a component with ng-model. I have plunkered my issue. I would like the component my-inp

2条回答
  •  终归单人心
    2021-02-11 22:55

    You can use this code:

    function myInputController($scope) {
        var self = this;
        this.$onInit = () => {
            this.ngModel.$render = () => {
                self.model = self.ngModel.$viewValue;
            };
            $scope.$watch(function() { return self.model; }, function(value) {
                self.ngModel.$setViewValue(value);
            });
        };
    }
    module.component('myInput', {
        require: {
            ngModel: '^ngModel'
        },
        template: '',
        controller: myInputController,
        controllerAs: 'vm'
    };
    

提交回复
热议问题