AngularJS从参数动态ng-model名称

时间:2018-10-19 09:00:07

标签: javascript html angularjs directive angular-ngmodel

修改

如何为此<input ng-model="this['newPlayer']['personal_info']['first_name']">设置 ['newPlayer']['personal_info']['first_name']是通过变量动态生成的?

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

1 个答案:

答案 0 :(得分:2)

您不必从属性中获取它,只需将其添加到指令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