具有ng-模型的Angular 1.5组件 [英] Angular 1.5 component with ng-model

查看:136
本文介绍了具有ng-模型的Angular 1.5组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将ng-model与组件一起使用?我想将范围变量绑定到具有ng-model的组件。我有抨击我的问题。我希望将my-input组件绑定到范围userData.name中的变量。

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-input to be binded to the variable from the scope userData.name.

我正在使用Angular JS 1.5.6组件,并且希望避免using指令。

I am using Angular JS 1.5.6 components, and want to avoid using directive.

<body ng-controller="MyCtrl">
  <div class="container">
    <h2>My form with component</h2>
    <form role="form">
      <div class="form-group">
        <label>First name</label>
        <my-input placeholder="Enter first name" ng-model="userData.name"></my-input>
      </div>
    </form>
  </div>
</body>


推荐答案

我修复了 plunker

参数名称必须与组件中的名称相对应。您应该在组件中使用camelCased名称,并在模板中使用kebab。示例:

Names of you parameters have to correspond to the names in your component. You should be using camelCased names in your component and kebab-cased in your templates. Example:

  bindings: {
      myPlaceholder: '@',
      myModel:'='
    }

 <my-input my-placeholder="Enter first name" my-model="userData.firstName">

关于使用ng的问题-model - 您可以在组件中定义任何参数。在这种情况下,参数的名称应为 ngModel

Regarding your question about using ng-model - you can use any parameter as far you define it in your component. In this case the name of your parameter should be ngModel.

这篇关于具有ng-模型的Angular 1.5组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆