Angularjs 通过指令更改ng模型

Angularjs 通过指令更改ng模型,angularjs,angularjs-directive,angularjs-controller,Angularjs,Angularjs Directive,Angularjs Controller,我正试图通过点击指令模板中的编辑锚来改变指令内部输入的ng模型 假设函数f()访问外部控制器并将editableValue绑定到name或company,以便我可以通过输入更改它 输入会显示persons值,但不会绑定到它 <p edit="person.name"></p> <p edit="person.company"></p> <input ng-model="editableValue"> main.controller(

我正试图通过点击指令模板中的编辑锚来改变指令内部输入的ng模型

假设函数f()访问外部控制器并将editableValue绑定到name或company,以便我可以通过输入更改它

输入会显示persons值,但不会绑定到它

<p edit="person.name"></p>
<p edit="person.company"></p> 
<input ng-model="editableValue">

main.controller('editsCtrl', function($scope){
    $scope.setToEdit = function(val){
        $scope.editableValue = val;
    }
}); 

main.directive('edit', function(){
return{
    template:'{{edit}}<a ng-click="f()"> Edit </a>',
    restrict:'A',
    scope:{
        edit:"="
    },
    replace:false,

    link: function(scope, element, attrs, ctrl) {
            scope.f = function(){
                  scope.$parent.setToEdit(scope.edit);
            }
          } 
    }
})

对于新手来说,这有点令人困惑,我遗漏了什么?

这里的范围分配失败:

scope.f = function(){
                  scope.$parent.setToEdit(scope.edit);
            }
因为您在这里限制了范围:

scope:{
        edit:"="
    },

Edit是父级将看到的唯一作用域,因为您已经使用“=”设置了双向数据绑定,这不是唯一的问题,而且它确实不必这样绑定,如果我可以执行类似于ng model=“editableValue['someproperty']”的操作,并根据链接属性值(当然点后)或类似设置$scope.someproperty,这将非常有效。您尝试过许多变体,比如“currentPerson[{{someproperty}}']”,并尝试过$parse inside指令。。。
scope:{
        edit:"="
    },