Angular 绑定未正确更新
Angular binding not updating properly
我想我想做的很简单。我正在尝试根据 select 框中的 selected 更新一些链接到控制器中对象的值。希望你们能指出我做错了什么。
我的控制器实例化对象(我不确定我是否真的需要这样做):
$scope.newInvoice = {}
然后我的 html 提供了一个下拉框,其中包含可以 select 编辑的发票列表,并且 table 应该 自动更新使用 selected 发票中的值,但它不会:
<tr>
<td>
<select ng-model="newInvoice">
<option ng-repeat="name in names" value="{{name}}">{{name.Name}}</option>
</select>
</td>
<td>TBD</td>
<td>{{newInvoice.PoNumber}}</td>
<td>{{newInvoice.Price | currency}}</td>
<td>
<input type="number" ng-model="newInvoice.Quantity"/>
</td>
<td>{{newInvoice.Price * newInvoice.Quantity | currency}}</td>
</tr>
本质上,table 不会填写 select 框中提供的值。但是,如果我使用 <pre>{{newInvoice}}</pre>
将 newInvoice
打印到屏幕上,我可以看到 newInvoice
实际上具有所有值,所有值都正确命名。所以看起来 select 框与模型结合得很好。有什么想法吗?
使用 ng-options
而不是使用 ng-repeat
的渲染选项,因为当您使用 ng-repeat
渲染 options
时,它会填充 value
属性 object
但那是 stringified
。就好像你在幕后使用 ng-options
它确实将实际对象值分配给提到的 ng-model
变量。
标记
<select ng-model="newInvoice" ng-options="name.Name for name in names"></select>
我想我想做的很简单。我正在尝试根据 select 框中的 selected 更新一些链接到控制器中对象的值。希望你们能指出我做错了什么。
我的控制器实例化对象(我不确定我是否真的需要这样做):
$scope.newInvoice = {}
然后我的 html 提供了一个下拉框,其中包含可以 select 编辑的发票列表,并且 table 应该 自动更新使用 selected 发票中的值,但它不会:
<tr>
<td>
<select ng-model="newInvoice">
<option ng-repeat="name in names" value="{{name}}">{{name.Name}}</option>
</select>
</td>
<td>TBD</td>
<td>{{newInvoice.PoNumber}}</td>
<td>{{newInvoice.Price | currency}}</td>
<td>
<input type="number" ng-model="newInvoice.Quantity"/>
</td>
<td>{{newInvoice.Price * newInvoice.Quantity | currency}}</td>
</tr>
本质上,table 不会填写 select 框中提供的值。但是,如果我使用 <pre>{{newInvoice}}</pre>
将 newInvoice
打印到屏幕上,我可以看到 newInvoice
实际上具有所有值,所有值都正确命名。所以看起来 select 框与模型结合得很好。有什么想法吗?
使用 ng-options
而不是使用 ng-repeat
的渲染选项,因为当您使用 ng-repeat
渲染 options
时,它会填充 value
属性 object
但那是 stringified
。就好像你在幕后使用 ng-options
它确实将实际对象值分配给提到的 ng-model
变量。
标记
<select ng-model="newInvoice" ng-options="name.Name for name in names"></select>