我怎样才能将json放入我的视图中?

时间:2015-09-02 07:03:26

标签: javascript json angularjs

我是使用JS的新手,我有一点问题,

我的应用程序正在使用Angular JS完成。我有我的服务/ api /服务,返回json与数据库中的所有用户。

我的工厂名为" UsersFactory.js"我的控制器叫" UsersController"

UsersController:

   var UsersController= function ($scope, $location, UsersFactory, $http) {
$scope.confirmDataForm = {
    emailAddress: 'email@email.com',
    firstName: 'Ryan',
    lastName: '',
    organization: '',
    organizationType: '',
    city: ''

};

UsersFactory:

  var ConfirmDataFactory = function ($http, $q) {
    return function (emailAddress, firstName, lastName, businessAddress,     organization, organizationType, city, state, zip, password) {

    var deferredObject = $q.defer();

    $http.post(
        '/Account/ConfirmData', {
            Email: emailAddress,
            FirstName: firstName,
            LastName: lastName,
            Organization: organization,
            OrganizationType: organizationType,
            BusinessAddress: businessAddress,
            City: city,
            State: state,
            Zip: zip,
            Password: password
        }
    ).
    success(function (data) {
        if (data == "True") {
            deferredObject.resolve({ success: true });
        } else {
            deferredObject.resolve({ success: false });
        }
    }).
    error(function () {
        deferredObject.resolve({ success: false });
    });

    return deferredObject.promise;
}
}

ConfirmDataFactory.$inject = ['$http', '$q'];

我想要一个包含所有用户名称的ComboBox,但我不知道如何。 (服务完成)

我的cshtml是这样的:

<select class="  " ng-model="confirmDataForm.city" id="city">
<option ng-repeat="todo in todos">
{{todos.text}}
</option>
</select>

谢谢!

1 个答案:

答案 0 :(得分:0)

我建议使用isteven multiselect。

Git:https://github.com/isteven/angular-multi-select

演示:http://isteven.github.io/angular-multi-select/#/main

简而言之:

<div
    isteven-multi-select
    input-model="namesOfAllUsers"
    output-model="pickedNames"
    button-label="icon name"
    item-label="icon name maker"
    tick-property="ticked"
>
</div

将用户放入$scope.namesOfAllUsers = [],这将列出您的所有用户,如果您选择一些用户,那么他们将被$scope.pickedNames = []数组中的用户填充。

如果您想仅限制选择一个用户名,请在div中添加selection-mode="single"

希望有所帮助