如何从数据库加载md-contact-chips?

时间:2016-05-06 06:54:01

标签: angularjs

app.controller("ProductPopupController", function ($scope, $http, $mdDialog, $filter) {
    console.log("ProductPopupController");

    var self = this;
    self.querySearch = querySearch;
    self.allContacts = loadContacts();
    self.contacts = [self.allContacts[0]];
    self.filterSelected = true;

    function querySearch(query) {
        console.log(4);
        var results = query ?
               self.allContacts.filter(createFilterFor(query)) : [];
        return results;
    }

    function createFilterFor(query) {
        console.log(5);
        var lowercaseQuery = angular.lowercase(query);
        return function filterFn(contact) {
            return (contact.name.indexOf(lowercaseQuery) != -1); ;
        };
    }

    function loadContacts() {
        console.log(6);
        var contacts = [

                    { 'name': 'one', 'email': '1@example.com', 'image': 'http://lorempixel.com/50/50/people?1' },
                    { 'name': 'two', 'email': '2@example.com', 'image': 'http://lorempixel.com/50/50/people?2' },
                    { 'name': 'three', 'email': '3@example.com', 'image': 'http://lorempixel.com/50/50/people?3' },
                    { 'name': 'four', 'email': '4@example.com', 'image': 'http://lorempixel.com/50/50/people?4' },
                    { 'name': 'five', 'email': '5@example.com', 'image': 'http://lorempixel.com/50/50/people?5' },
                    { 'name': 'six', 'email': '6@example.com', 'image': 'http://lorempixel.com/50/50/people?6' },
                    { 'name': 'sixteen', 'email': '7@example.com', 'image': 'http://lorempixel.com/50/50/people?7' },
                    { 'name': 'seven', 'email': '8@example.com', 'image': 'http://lorempixel.com/50/50/people?8' }
               ];
        return contacts;
    }

});

以上角度代码在我的应用程序中完美运行。这里硬编码的联系人正在使用绑定;如何替换它并从数据库访问。 get请求如下所示。

$http.get('UserDetail/GetAllUsers').success(function (response) {
        console.log("I got the data I requested");
        console.log(2);

    });

如何实现这个?

2 个答案:

答案 0 :(得分:0)

类似的东西:

function loadContacts() {    
    $http.get('UserDetail/GetAllUsers').success(function (response) {
            self.allContacts = response;
        });  
}

答案 1 :(得分:0)

您可以尝试在控制器中使用$ q服务:

function.loadContacts = function () {
      deferredUsers = $q.defer();

     $http.get('UserDetail/GetAllUsers').success(function (response) {
            self.allContacts = response;
            deferredUsers.resolve(self.allContacts);
        });