Angularjs 指令中的双向绑定,来自服务的模型不可分配

Angularjs 指令中的双向绑定,来自服务的模型不可分配,angularjs,angularjs-directive,angularjs-service,Angularjs,Angularjs Directive,Angularjs Service,我得到了以下错误:“与指令“{1}”一起使用的表达式“{0}”是不可赋值的!”,如下所示:$compile/nonassign 编辑:已将配置对象移动到范围中,此部分已解决,但模型仍未按预期绑定 老普朗克: (尝试编辑字段并检查控制台) 我真的不明白这个问题,这就是为什么链接不能帮助我解决它。我使用一个指令来绑定数据,而被“绑定”的数据实际上指向一个服务 <multi-edit model="profileService.current" config="{ title: 'Edi

我得到了以下错误:“与指令“{1}”一起使用的表达式“{0}”是不可赋值的!”,如下所示:$compile/nonassign

编辑:已将配置对象移动到范围中,此部分已解决,但模型仍未按预期绑定

老普朗克: (尝试编辑字段并检查控制台)

我真的不明白这个问题,这就是为什么链接不能帮助我解决它。我使用一个指令来绑定数据,而被“绑定”的数据实际上指向一个服务

<multi-edit model="profileService.current" config="{
    title: 'Edit profile description', 
    fields: [
    {name: 'Title', model: profileService.current.title, input: true}, 
    {name: 'Description', model: profileService.current.description, textarea: true}
]}">
  CLick me
</multi-edit>

点击我

因此,我尝试编辑profileService.current.title,例如,使用ng模型配置字段[0].model。它可以正确读取数据,但不能写入数据。我需要做什么才能写入正确的模型?

已更新

您应该为“config”使用范围模型

将其放入主控制器:

$scope.config = {
  title: 'Edit profile description',
  fields: [{
    name: 'Title',
    model: 'title',
    input: true
  }, {
    name: 'Description',
    model: 'description',
    textarea: true
  }]
};
然后在主HTML中:

<multi-edit model="profileService.current" config="config">
  CLick me
</multi-edit>

点击我
然后,在指令HTML中:

<li ng-repeat="(key, value) in config.fields">
    <input ng-if="value.input" type="text" ng-model="model[config.fields[key].model]" />
    <textarea ng-if="value.textarea" type="text" ng-model="model[config.fields[key].model]"></textarea>
</li>

  • 请参阅更新的。

    Duh!我这样做只是为了测试它是否能工作,考虑在测试后将其移动到控制器。所以您解决了这个问题,但是编辑器ng模型并没有反映原始ng模型,正如在这里可以观察到的那样:所以我想这只解决了第一个问题(谢谢!)。因为
    profileService.current
    在您的指令的控制器中没有更新;但是
    scopeConf.fields
    model
    字段被更新。在index.html中尝试使用绑定数据的
    model:{{scopeConf.fields[1].model}
    ,您将看到数据将发生变化。那么有没有办法确保profileService.current正确绑定到conf/指令?您可以使用双向绑定到
    profileService.current
    model
    对象,在你的指令中。请看我更新的答案;问题是我现在被锁定在使用model.title和model.description。另一个对象可能没有描述,而只是用其他数据填充。我试图尽可能地概括,以便能够使用不同类型的数据重用该指令,这就是配置功能的原因。