动态地将Angular模型分配给输入

Div*_*vey 2 javascript angularjs angularjs-directive angularjs-scope

我正在尝试为角应用创建一个迷你电子表格.我想重新创建一个通用电子表格功能,允许用户单击电子表格单元格,然后使用工作表顶部的较大输入更改该单元格的值.理想情况下,当用户点击其中一个单元格时,我想将给定单元格的模型分配给大型输入,但我无法确定如何执行此操作.

有一些更精细的细节可以解决细胞的模糊和聚焦问题.而且,我给出的例子大大简化了; 可以有任意数量的行和列.我的主要问题是:如何动态地将单元格模型分配给大输入,以便它可以作为单元格的一种代理输入?如果这不可能/不实用,有没有更好的方法来处理这个?

这就是我到目前为止所拥有的.我甚至不知道这是否可行,特别是我采用的这种方法.有任何想法吗?

http://plnkr.co/edit/6tTsilCGSepYyCfbvidp?p=preview

的index.html

<table ng-controller="SpreadsheetCtrl" custom-spreadsheet>
  <thead>
    <tr>
      <th colspan="3">
        <input type="text" style="width: 100%" />
      </th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="row in rows">
      <td>
        <input ng-model="row.A" type="text" />
      </td>
      <td>
        <input ng-model="row.B" type="text" />
      </td>
      <td>
        <input ng-model="row.C" type="text" />
      </td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

的script.js

var app = angular.module('app', []);

app.controller('SpreadsheetCtrl', function($scope) {
  $scope.rows = [
    {A: 'a', B: 'b', C: 'c'},
    {A: 'a', B: 'b', C: 'c'},
    {A: 'a', B: 'b', C: 'c'}
  ];
}); 

app.directive('customSpreadsheet', function () {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      var primary = element.find('thead input');
      element.on('focus', 'tbody input', function () {
        // of course, this won't work! but it shows the (basic) idea
        // of what I'm trying to do
        primary.attr('ng-model', $(this).attr('ng-model'));
      });
    }
  };
})
Run Code Online (Sandbox Code Playgroud)

Zac*_*yle 5

我会抛弃指令并利用ng-click.这是一个工作的PLUNKER.

注意

<input ng-model="row.A" type="text" ng-click="choose(row, 'A')"/>
Run Code Online (Sandbox Code Playgroud)

<input type="text" ng-model="selected[attr]" style="width: 100%" />
Run Code Online (Sandbox Code Playgroud)

$scope.choose = function(row, attr) {
    $scope.selected = row;
    $scope.attr = attr;
}
Run Code Online (Sandbox Code Playgroud)