S
S
Sergei2016-03-27 23:40:49
Angular
Sergei, 2016-03-27 23:40:49

How to solve problem with default values ​​in select angular?

Hello. Started learning angular and ran into a problem.
I have 2 objects in scope:

1)
tasks:
    12:
        id: 12
        user_id: 1
и т.д.

2)
users:
    1:
        id: 1
        username:  sergey

I'm trying to display select:
<select class="form-control"
    data-ng-model="tasks[task.id].user_id"
    data-ng-change="setWorker(task.id)"
>
    <option data-ng-repeat="user in users" data-ng-value="user.id" data-ng-selected="{[{user.id == task.user_id}]}">{[{user.username}]}</option>
</select>

But there are 2 problems:
1) By default, in this example, sergey is not put in the
select 2) An option of the form is added to the select:
<option value="? number:4 ?"></option>
Initially, I sinned that when loading data from the server in $scope.users and $scope.tasks, in addition to the data, there was a promise , but I got rid of this by doing after receiving the data:
$scope.users = data.toJSON();
There is also another 3 problem:
After updating the task data and successfully completing the ajax request on the server, for example, changing the user_id, I change the data in the scope:
$scope.tasks[taksId].user_id = userId ;
And I would like my $scope.test() function to be executed when changing $scope.tasks;
Added watcher to tasks:
$scope.$watch('tasks', function () {
    $scope.test();
 })

But there is no effect. Please help me solve these problems. Does anyone have any options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kumanin, 2016-03-28
@jackkum

Apparently it should be like this ?
2. because of that. that users is not an array but an object.
3. for arrays you need to use $watchCollection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question