模型在二维数组变量

模型在二维数组变量

本文介绍了NG-模型在二维数组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  <p ng-repeat="row in matrix">
    <span ng-repeat="column in row">
      <input type="text" style="width: 20px; text-align: center;" ng-model="column" ng-change="{{column = }}">
    </span>
  </p>

和我的控制器:

$scope.matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];

我有小片code的,我想那个小文本输入框与矩阵有关[I] [U] 。我知道我可以使用 NG-模型= 来使文本框与某个变量相关联。

I have little piece of code and I want that little text input box to be associated with matrix[i][u]. I know I can use ng-model= to make the text box be associated with a certain variable.

不过,我希望它是双向的 - 改变变量将改变文本框的值和改变文本框的值将改变变量。然而,当我有 NG-模型上输入文本框,我似乎不能编辑它的价值,因为它永远重置为其默认值。

However, I want it to go both ways - changing the variable will change the text box value and changing the text box value will change the variable. However, when I have ng-model on an input text box I can't seem to edit its value, as it'll always "reset" to its default.

我知道我可以使用 NG-变化,但我所做的中间线这样的:

I know I can use ng-change but I made the middle line this:

<input type="text" style="width: 20px; text-align: center;" ng-model="column" ng-change="update">

和它没有工作调用$ scope.update()函数。我也还是不能编辑文本框中的值。

And it didn't work to call the $scope.update() function. I also still can't edit the text box value.

TL;博士:我怎样才能有一个文本框NG-模型并允许编辑它来编辑这两个文本框的值和NG-模型中的变量

tl;dr: How can I have a text box with an ng-model and allow editing it to edit both the text box value and the variable on ng-model.

推荐答案

使用这种方式:

<input type="text" ng-model="matrix[$index][$parent.$index]" style="width: 20px; text-align: center;" />

例如:

这是不是很优雅,但它的工作。

It is not very elegant , but it is working..

这篇关于NG-模型在二维数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 12:30