【问题标题】:ngModel, set a whole object and not only its value from ngOptiosngModel,设置整个对象,而不仅仅是来自 ngOptios 的值
【发布时间】:2013-07-15 06:35:19
【问题描述】:

我只是,不知道如何设置我的ng-model 以包含选择的值,而不是对象本身。

HTML

<select name="status"
        data-ng-model="search.status"
        data-ng-options="s.label for s in statuses">
</select>

$scope.statuses = [
    { label: ' - si/no - ', value: '' },
    { label: ' Migrated ', value: 0 },
    { label: ' Active ', value: 1 },
    { label: ' Canceled ', value: 2 },
    { label: ' Un-confirmed ', value: 3 },
];

$scope.search.status = $scope.statuses[0];

结果是:

stdClass Object
(
    [label] =>  - si/no - 
    [value] => 
)

现在,我只想要[value],而不是整个对象。我怎样才能做到这一点?

附:我不想访问object-&gt;value,我只希望我的x 变量已经包含该值。

更新

我以前有过这个,它正在工作:

<select name="status" class="txt" data-ng-model="search.status">
    <option value=""> - ALL - </option>
    <option value="0"> Migrated </option>
    <option value="1"> Active </option>
    <option value="2"> Canceled </option>
    <option value="3"> Un-confirmed </option>
</select>

我的search.status 将只包含选定的值,而不是像上面代码那样包含整个对象。

【问题讨论】:

  • $obj-&gt;value 是您要查找的符号。
  • 我的错,不够清楚。我不想访问object-&gt;value,我只希望我的x 变量已经包含该值。
  • 你的 x 变量是什么...也许你的意思是 $x = $x-&gt;value;$x = $obj-&gt;value; 我想我们可能需要查看更多代码才能提供指导
  • 让我重新表述一下。我希望我的$x 变量已经包含$scope.statuses[N].value,并且稍后必须实际访问该对象,因此最终可以到达我的value。对我来说,$scope.search.status 应该只发送 value 而不是它自己的对象
  • 好吧...我想你想要$scope.search.status = $scope.statuses[0].value;

标签: php javascript object angularjs html-select


【解决方案1】:

您需要更改标记。 ng-options 表达式中的 as 关键字具有魔力。

下面的表达式将对象属性s.value 设置为模型,但s.label 仍然作为&lt;option&gt; 元素的标签(我认为这是您想要的)。

<select name="status"
        ng-model="search.status"
        ng-options="s.value as s.label for s in statuses">
</select>

当然,必须修改控制器中的模型值初始化(如@Orangepill 所述):

$scope.search.status = $scope.statuses[0].value;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多