简体   繁体   English

ng-select AngularJS

[英]ng-select AngularJS

I'm trying to put together an ng select to select these quantities: 我正在尝试将ng选择放在一起以选择这些数量:

   "splits":[  
      1,
      2,
      3,
      4,
      5,
      6,
      8
   ],


<select  ng-options="?" ng-model="ticket_group.quantity" </select>

Then I want to show the price multiplied by the quantity they have chose 然后我想显示价格乘以他们选择的数量

 <div style="text-align: center;font-size:25px;"><b> ${{ticket_group.retail_price * ticket_quantity}} USD</b>

So would I just reference the ng-model for this? 那么我只是参考ng模型吗?

Please help me figure out ng-select and have it display the final price 请帮我弄清楚ng-select并让它显示最终价格

ng-model is not the problem, considering you expect $scope.ticker_group.quantity to hold the value at the end. 考虑到你期望$scope.ticker_group.quantity保持最后的值, ng-model不是问题。

What you need to set is ng-options . 您需要设置的是ng-options First of all, those options must be in the scope: 首先,这些选项必须在以下范围内:

$scope.splits = [
    1,
    2,
    3,
    4,
    5,
    6,
    8
];

This is an example. 这是一个例子。 You used a key (ie splits belonged to a literal object) so you must ensure the object having such key belongs (directly or not) to the $scope . 您使用了一个键(即属于文字对象的splits ),因此您必须确保具有此类键的对象(直接或不是)属于$scope

Following MY example, splits is now in the scope, so I can use it as data-binding expression in the ng-options . 在我的示例之后, splits现在在范围内,因此我可以将其用作ng-options中的数据绑定表达式。

ng-options expects key:value are specified, so the syntax is: ng-options期望key:value被指定,因此语法是:

ng-options="foo as bar for (foo, bar) in anObject"

So if you have any object, you can iterate keys (properties) and values. 因此,如果您有任何对象,则可以迭代键(属性)和值。 notice that foo and bar are examples (you can use any name, being sure such names are not reserved words), where foo is declared in the place to define the keys (ie the value attribute of each option object), and bar is declared in the place to define labels for such options. 请注意foobar是示例(您可以使用任何名称,确保此类名称不是保留字),其中foo在定义键的位置声明(即每个option对象的value属性),并声明bar在为这些选项定义标签的地方。

As an alternative, if you iterate an array object instead of other classes object, you can specify like this: 作为替代方案,如果迭代数组对象而不是其他类对象,则可以像这样指定:

ng-options="foo as foo for foo in anArray"

which will generate options having the value as the label (same contents). 这将生成具有值作为标签的选项(相同的内容)。 You will use them like this: 你会像这样使用它们:

ng-options="val as val for val in splits"

AND if you have a case where you have an array of objects and want to specify value and label from properties of such objects, like: 如果您有一个对象数组并希望从这些对象的属性中指定值和标签的情况,例如:

$scope.people = [{dni:32111269, nombre:"Pepe Galleta"}, {dni:11111111, nombre:"Nestor Kirchner"}, {dni:12345678, name:"Mi Abuela"}, {dni: 23456789, nombre:"Tu Hermana"}];

And want to make dni as the value, and nombre as the label, You'd use: 并希望将dni作为值,并将nombre作为标签,您将使用:

ng-options="foo.dni as foo.nombre for foo in people"

See more here: https://docs.angularjs.org/api/ng/directive/select 点击此处查看: https//docs.angularjs.org/api/ng/directive/select

Who will hold the value? 谁将保持价值? : The expression you set in the ng-model directive will be the holder of the selected value once you use the selector. :使用选择器后,在ng-model指令中设置的表达式将成为所选值的持有者。

You want, perhaps, to use a: 或许你想要使用:

$scope.$watch('splits', function(v){
    ...
});

to show the price being modified by the value on splits . 显示splits价值修改的价格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM