如何设置 <select> 的初始 ng-model 值以传递给函数而不需要更改 select?

How to set the initial ng-model value for a <select> to pass to function without needing to change the select?

很抱歉,如果这个问题已经得到解答,但我已经查看了很多问题,但无法正确回答。

使用带有以下声明的 angulaJS 从 JSON 对象创建我的 select。 select 完美创建并且使用 ng-selected 选项正确设置了项目。

<select ng-model="assignment.status">
            <option ng-selected="instanceData.status.current" ng-repeat="status in instanceData.status.list" value="{{status.id}}">{{status.title}}</option>
</select>

我遇到的问题是,当我对传递赋值 (ng-model) 值的 ng-click 函数执行更新时,除非 select改变了。

<a class="button button-positive" ng-click="save_status_assignment(assignment)">Update</a>

有没有人可以帮助我?

非常感谢 B

您只需默认为$scope.assignment.status赋值即可。

现在,那个变量是空的,所以它不会给你任何东西。 所以在你的控制器中这样的东西会起作用

$scope.assignment = {};
// Assuming you want the default to be the first value
$scope.assignment.status = $scope.instanceData.status.list[0].id;

那你就可以开始了。

编辑

此外,您应该使用 ng-options 而不是 ng-repeat 作为您的选项。

https://docs.angularjs.org/api/ng/directive/ngOptions