angularJS fill选择Json数据并选择所选值

时间:2015-12-22 13:30:00

标签: javascript angularjs json

我有2个实体。 project和project_types。 一个项目有一个或多个project_types。

通过ajax(ng-init)我得到项目数据(与该项目的相关project_types) 和所有project_types(用于为项目选择一个或多个project_types):

$scope.showProject = function() {
        $http.post($scope.apiUrl+'/show', {
            projectslug: $attrs.projectslug
        }).success(function(data, status, headers, config) {
            $scope.project = data;
        });

        $http.post($scope.apiProjectTypesUrl+'/index', {
        }).success(function(data, status, headers, config) {
            $scope.project_types = data;
        });
    };

所以在{{project}}范围内有这个json:

{"id":1,"slug":"test","active":1,"name":"Test","description":"dgdgdgdg","from":"2015-12-22 00:00:00","to":"2015-12-22 00:00:00","created_by":1,"updated_by":1,"created_at":"2015-12-22 12:08:47","updated_at":"2015-12-22 12:08:47","deleted_at":null,"from_date_time":"22.12.2015","to_date_time":"22.12.2015","formatted_description":"dgdgdgdg","users":[],"project_types":[{"id":4,"slug":"event","active":1,"name":"Event","description":"","created_by":0,"updated_by":0,"created_at":"2015-12-22 09:49:43","updated_at":"2015-12-22 09:49:43","deleted_at":null,"pivot":{"project_id":1,"project_type_id":4,"created_at":"2015-12-22 13:13:12","updated_at":"2015-12-22 13:13:12"}}]}

并且在{{project_types}}范围内有这个json:

[{"id":4,"slug":"event","active":1,"name":"Event","description":"","created_by":0,"updated_by":0,"created_at":"2015-12-22 09:49:43","updated_at":"2015-12-22 09:49:43","deleted_at":null},{"id":5,"slug":"sfsfsfsfsfsfsfsfsfsf","active":1,"name":"sfsfsfsfsfsfsfsfsfsf","description":"","created_by":0,"updated_by":0,"created_at":"2015-12-22 09:50:23","updated_at":"2015-12-22 09:50:23","deleted_at":null},{"id":6,"slug":"eteett","active":1,"name":"eteett","description":"","created_by":0,"updated_by":0,"created_at":"2015-12-22 10:07:55","updated_at":"2015-12-22 10:07:55","deleted_at":null},{"id":7,"slug":"sfssfsf","active":1,"name":"sfssfsf","description":"","created_by":0,"updated_by":0,"created_at":"2015-12-22 12:36:37","updated_at":"2015-12-22 12:36:37","deleted_at":null}]

在我的html中,我创建了选择(多个)字段:

<select id="projectTypes"
   class="form-control"
   name="projectTypes[]"
   ng-model="project.project_types"
   ng-options="project_type.id as project_type.name for project_type in project_types"
   multiple="multiple">
</select>

所以,在我的选择字段中有4个选项(比如在project_types json中 - 这是正确的)

但是如何在选择字段中从我的项目中选择正确的关系? ng-model =“project.project_types”包含这个json:

[{"id":4,"slug":"event","active":1,"name":"Event","description":"","created_by":0,"updated_by":0,"created_at":"2015-12-22 09:49:43","updated_at":"2015-12-22 09:49:43","deleted_at":null,"pivot":{"project_id":1,"project_type_id":4,"created_at":"2015-12-22 13:13:12","updated_at":"2015-12-22 13:13:12"}}]

我无法让它工作......我的选择字段中默认不会选择任何值...

谢谢你让我走向正确的方向!

1 个答案:

答案 0 :(得分:0)

您是否尝试过&#34;?

添加&#34;曲目?
 ng-options="project_type.id as project_type.name for project_type in project_types track by project_type.id"

如果您有一个可用的代码片段或小提琴,这将更容易提供帮助。