ng-model 中的传递函数 [英] Pass function in ng-model

查看:28
本文介绍了ng-model 中的传递函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将函数传递给ng-model,例如

Is it posible to pass function into ng-model, for example

<input type="text" name="email" class="form-control" ng-model="createModel('email')" ng-change="addProperty(email,'email')" email required placeholder="Email">

ng-change 工作正常,但 ng-model="createModel(email)" 显示此错误

ng-change is working fine, but ng-model="createModel(email)" is showing this error

> Expression 'createModel('email')' is non-assignable. Element: <input
> type="text" name="email"....

在控制器中我有://我现在只想传递值

In controler i have : // I just want to pass value for now

  $scope.createModel = function(modelName){
     console.log("Model name"+modelName);
  }

我在互联网上看到人们这样做的例子

I saw examples on the internet that people doing this

推荐答案

看起来 AngularJS 在 1.3 版本中添加了getter"setter"支持

Looks like AngularJS added "getter" "setter" support in version 1.3

您可以滚动到他们的 ngModel 文档页面底部:

You can scroll to the bottom of their ngModel documentation page at:

https://docs.angularjs.org/api/ng/directive/ngModel

这允许您在 ngModel 属性中指定方法而不是变量.该方法应采用可选参数.如果传递了参数,它应该存储该值,如果没有传递参数,它应该返回一个值.

This allows you to specify a method instead of a variable in your ngModel attribute. The method should take an optional parameter. If an argument is passed it should store that value, if no argument is passed it should return a value.

您可以在另一个 Stack Overflow 回答中看到一个示例:https://stackoverflow.com/a/28224980/984780

You can see an example in another Stack Overflow answer at: https://stackoverflow.com/a/28224980/984780

这篇关于ng-model 中的传递函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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