如何将组合值设置为 ng-init

How to set combined value to ng-init

我想给 ng-init 赋值=d.id+d.concept_class 就像 ng-init="m_concept_class=12 abc"

<div ng-repeat="d in data">

  <input type="text" ng-model="m_concept_class" 
        ng-init="m_concept_class={{d.id; d.concept_class}}" >    
</div>

当我使用这种语法时 ng-init="m_concept_class={{d.id}} {{d.concept_class}}" 我可以在浏览器的检查元素功能中看到数据“12 abc”,但没有显示在文本框中。

您不需要使用插值,只需使用连接即可。

使用

m_concept_class=d.id  + ' ' +d.concept_class

您可以简单地添加它:-)

 <div ng-repeat="d in data">

  <input type="text" ng-model="m_concept_class" 
        ng-init="m_concept_class=d.id +d.concept_class" />    
</div>

Fiddle

在初始化输入元素时将值设置为使用 ng-value 而不是 ng-init

<div ng-repeat="d in data">
  <input type="text" ng-model="m_concept_class" ng-value="m_concept_class=d.id +' '+ d.concept_class" >    
</div>

你也可以使用 ng-init,但 ng-value 会更可取,它类似于 html 的 value 属性,它将为输入元素设置值并绑定到ng-模型变量。

注意

Don't use {{}} interpolation directive inside ng-value or ng-init as they only support expressions.