【问题标题】:how to use ng-model on input component in angularjs如何在angularjs中的输入组件上使用ng-model
【发布时间】:2018-10-16 15:44:23
【问题描述】:

这是我的html文件代码

<form name="myForm" ng-controller="ExampleController" novalidate="">
<email classname="form-control" name="Email" type="email" modelname="email_id" 
placeholder="Email Address" formref="myForm" ></email>
<button ng-click="Click()">click</button>

我希望当我点击按钮时,它给了我电子邮件的值,使用 ng-model(email_id) 的值 -

$scope.Click = function(){
    console.log($scope.email_id);
  };

这是我的组件代码

angular.module('textInputExample', []).component("email", {
template:
        '<input type="{{$ctrl.type}}" class="{{$ctrl.classname}}" 
placeHolder="{{$ctrl.placeholder}}" name="{{$ctrl.name}}" ng- 
model="$ctrl.modelname" required>' +
        '<div ng-messages="$ctrl.formref[$ctrl.name].$error" ng- 
show="$ctrl.formref[$ctrl.name].$touched">' +
        '<p ng-message="required">Providing a {{$ctrl.placeholder}} is 
mandatory.</p>' +
        '<p ng-message="email">{{$ctrl.placeholder}} is invaild</p></div>',

bindings: {
    formref: '<',
    name: '@',
    placeholder: '@',
    classname: '@',
    type: '@',
    modelname:'='
}

在上面的组件代码中,我使用该模型名称值分配给 ng-model 属性,然后我想在控制器中使用该值来访问输入字段值,但我没有将 ng-model 设置为我的值我正在分配它(email_id),我也无法访问输入字段的值。 请帮助我该怎么做。 如果需要任何信息来重新评分此问题以解决问题,请通知我。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    我找到了我的解决方案。如果有人遇到同样的问题,这是我的解决方案-

    把这段代码写到你的js文件里

    component("email", {
    bindings: { ngModel: '<' },
    require: { ngModelCtrl: 'ngModel' },
    template:
            `<input type="{{$ctrl.type}}" class="{{$ctrl.classname}}" placeHolder="{{$ctrl.placeholder}}" name="{{$ctrl.name}}" ng-model='$ctrl.ngModel'
                 ng-change="$ctrl.ngModelChange()" required>
            <div ng-messages="$ctrl.formref[$ctrl.name].$error" ng-show="$ctrl.formref[$ctrl.name].$touched">
            <p ng-message="required">Providing a {{$ctrl.placeholder}} is mandatory.</p>
            <p ng-message="email">{{$ctrl.placeholder}} is invaild</p></div>`,
    
    bindings: {
        formref: '<',
        name: '@',
        placeholder: '@',
        classname: '@',
        type: '@',
        modelname:'='
    },
    controller: function() {
      this.ngModelChange = () => {
        this.ngModelCtrl.$setViewValue(this.ngModel);
      };
    }
    

    这里是html文件代码

    <email classname="form-control" name="Email" type="email" ng-model="email_id" 
    placeholder="Email Address" formref="myForm" ></email>
    

    感谢大家对我的帮助

    【讨论】:

      【解决方案2】:

      <input type="{{$ctrl.type}}" class="{{$ctrl.classname}}" placeHolder="{{$ctrl.placeholder}}" name="{{$ctrl.name}}" ng- model="$ctrl.modelname" required>

      尝试绑定 ng-model,然后您可以从/向 HTML 文件获取值。尝试使用以下 sn-p 更改您的代码。

      <input type="{{$ctrl.type}}" class="{{$ctrl.classname}}" placeHolder="{{$ctrl.placeholder}}" name="{{$ctrl.name}}" [(ngModel)]="$ctrl.modelname" required>

      【讨论】:

      • 您的代码没有效果,但感谢您的回答
      • 为什么?你能详细说明一下吗
      • 我试过这段代码但还是不行,谢谢你的努力
      猜你喜欢
      • 2016-03-08
      • 2019-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多