Angular在函数启动后注入一个模块

时间:2016-03-01 13:53:09

标签: javascript jquery angularjs ajax inject

好的,第一个问题是......好心..

我正在使用Angular的Material框架进行Angular Autofill输入。

我需要用来自ajax调用的数据填充它。 (即一个国家arrey) 我最初的方法是将模块注入应用程序的控制器中,然后当ajax调用返回成功时,我只需启动它的功能。

这不能很好地工作,因为它会在数据恢复之前加载一次(从注入开始)并在检查时崩溃。

拿两个:添加对空数据的检查导致它按预期加载和停止,然后当数据到达时(最终)模块在单个项目中切割它们并准备就绪但页面不能正常工作。由于不工作,我的意思是它不会自动建议你输入..

我认为我需要加载模块而不是其中的函数但我似乎无法做到这一点..这就是我需要帮助的地方..

添加代码:

angular.module("Myapp", ['ngMaterial', 'Autofill-country' ])
 .controller("mycontroller", ["$scope", "$http", function ($scope, $http) {

    //automatic suggest
     automaticsuggest ()
  function automaticsuggest() {
    suggesturl = 'http://www.mysuggest_front_services'
    $http({
        method: 'GET',
        url: suggesturl,
        responseType: "json",
        async: false // Just tried that to see if it works.. I am not really fond of it 
    }).then(function (response) {
        countriesarrey = response.data.Countries

        // .run('Autofill-country', ['Autofill-country']) - Tried this too..
        country(); // this is the function of the autosuggest. 
Once checked with breakpoints it does get the array and cut it and filter it etc, but the n when you input something in the input field.. It doesnt suggest..            
    });

  }

自动完成模块

angular.module('Autofill-country', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
  .controller('country', country);

function country($timeout, $q) {
var self = this;


if (countriesarrey == "undefined" || countriesarrey == null) { }
else { countrys(); }
// list of `country` value/display objects

function countrys() {

    self.countries = loadAll();
    self.selectedItem = null;
    self.searchTextcountry = null;
    self.querySearch = querySearch;

    // ******************************
    // Internal methods
    // ******************************

    /**
     * Search for countries... use $timeout to simulate
     * remote dataservice call.
     */
    function querySearch(query) {
        var results = query ? self.countries.filter(createFilterFor(query)) : [];
        return results;
    }

    /**
     * Build `countries` list of key/value pairs
     */

    function loadAll() {
        var allcountries = countriesarrey;
        console.log(countriesarrey);
        return allcountries.split(/, +/g).map(function (country) {
            return {
                value: country.toLowerCase(),
                display: country
            };
        });
    }

    /**
     * Create filter function for a query string
     */
    function createFilterFor(query) {
        var lowercaseQuery = angular.lowercase(query);

        return function filterFn(country) {
            return (country.value.indexOf(lowercaseQuery) === 0);
        };

    }
 }
}

1 个答案:

答案 0 :(得分:1)

您应该创建一些工厂来获取您的数据,这将有一个方法,返回承诺。在承诺解决后,您可以使用获取的数据执行所需的操作。

var myApp = angular.module('myApp', []);

myApp.factory('myFactory', function($http, $q, $timeout) {
    return {
        getData: function() {
            // return $http.get(...);
        },
    };
});

myApp.controller('MyCtrl', function(myFactory) {
    this.getData = function() {
        myFactory.getData().then(function(response) {
            // use response.data here
        })
    }
})

请参阅小提琴:http://jsfiddle.net/jcpmsuxj/44/