【问题标题】:AngularJS dynamically ng-model name from parameterAngularJS从参数动态ng-model名称
【发布时间】:2019-03-24 03:36:33
【问题描述】:

编辑

如何设置这个<input ng-model="this['newPlayer']['personal_info']['first_name']"> ['newPlayer']['personal_info']['first_name'] 动态来自变量?

类似这样的:<input ng-model="this[variable]">

【问题讨论】:

  • 不明白“硬编码”是什么意思
  • 我已经编辑了问题
  • 所以你不再需要它在自定义组件的范围内,是吗?
  • 我需要 - 但我知道如何将我需要的模型名称发送到子指令/组件。我只是不知道如何在 html 中应用它......我可以到console.log($attrs.whatEverISent),结果会很好。但是如何将whatEverISent发送到实际输入标签中的ng-model指令呢?

标签: javascript html angularjs directive angular-ngmodel


【解决方案1】:

您不必从属性中获取它,只需将其添加到您的指令require: 'ngModel' 像这样

app.directive("textInput", () => {
  return {
    templateUrl: "/text-input.html",
    require: 'ngModel'
    scope: true,
    link: function($scope, $element, $attrs, ngModel) {
      angular.element($element).append($scope[$attrs.myModel]);
    }
  }
});

对于链接函数中的每个 textInput 实例,您都有不同的 ngModel

【讨论】:

  • No.. 这不是问题 - 在指令的 HTML 中写什么?
  • 我还是通过$attrs 和required 键获得了数据-但是如何将值分配给ng-model?
  • 查看这篇关于在您的自定义组件中使用ngModel 的文章:codelord.net/2017/07/28/youre-not-using-ng-model-enough
猜你喜欢
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多