【发布时间】:2017-08-04 15:54:12
【问题描述】:
我知道已经有很多关于这个主题的问题,我尝试了我找到的解决方案,但它似乎不起作用。 基本上,我有一个名为 basicMunch 的指令,它遍历对象数组并创建一些 html。 除了 ng-modal 之外,一切都运行良好并绑定 这是指令的代码:
> munches.directive('basicmunches', function() { return {
> require: 'ngModel',
> template:`<div class='columns'> <div ng-repeat="mu in basicMunches" class="card column col-4 col-sm-12">
> <div class="card-header">
> <h4 class="card-title">{{mu.name}}</h4>
> <h6 class="card-subtitle">{{mu.type}}</h6>
> </div>
> <div class="card-body">
> {{mu.text}}
> </div>
> <div class="card-footer">
> <div class="form-group">
> <label class="form-switch">
> <input ng-model="mu.tag" name="{{mu.tag}}" type="checkbox" />
> <i class="form-icon"></i> Turn On
> </label>
> </div>
> </div>
> </div></div>` } });
这是数组:
$scope.basicMunches = [
{"name":"The New York TImes",
"text":"Get the top headlines from the NYT every morning",
"type":"News", "tag":"nyt"
},
{"name":"Wall Street Journal",
"text":"Get the top headlines from the WSJ every morning",
"type":"News", "tag":"wsj"
}
];
我已经尝试过 $parent.mu.tag 和 $parent.$parent.mu.tag,但这并没有奏效,因为它没有嵌套在其他范围内(至少我不知道)
我试着做controller.mu.tag的名字,也不管用
我试过做 mu['tag'] 也不管用
我尝试使用 {{ 但这不起作用 我将其更改为 ng-bind={{mu.tag}} 并且确实有效 对我来说很奇怪它适用于 ng-bind 但它不适用于 ng-model.... 无论如何,有人有什么想法吗?
【问题讨论】:
-
您的 ng-model="mu.tag" 绑定到一个字符串,输入是 type="checkbox" 不起作用。将类型更改为文本或绑定到布尔值。
-
它似乎在 plunker 中工作 here,你能在这里重现问题吗..
-
@PankajParkar 是的,该脚本正在运行,但这并不是我真正想要做的。如果您检查元素输入,您将看到我所询问的问题仍然存在,ng-model 仍然说它等于“mu.tag”,而不是我想要的标签名称
-
您尝试做的事情很奇怪。您真的想要
ng-model="wsj",通过从mu.tag属性读取值wsj,是吗?虽然可能有办法让这成为可能,但就这个数组而言,它并没有真正的意义,因为这意味着你在$scope上有一个名为wsj的属性,以及如何这是否与mu的其余部分有关,特别是因为可以有多个mu但只有一个wsj属性? -
是的,这正是我想做的。每个 mu 标签都有不同的属性,wsj 只是一个例子。 @克莱斯
标签: javascript angularjs angularjs-ng-model ng-bind