【问题标题】:Angular inject a module after a function starts函数启动后Angular注入模块
【发布时间】:2016-06-13 23:33:06
【问题描述】:

好的,第一个问题,所以......善良..

我正在使用 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);
        };

    }
 }
}

【问题讨论】:

    标签: javascript jquery angularjs ajax inject


    【解决方案1】:

    您应该创建一些工厂来获取您的数据,这将有一个方法,返回承诺。在 promise 解决后,你可以对获取的数据做你想做的事情。

    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/

    【讨论】:

    • 我还没有完全尝试过,但它看起来真的很有希望(ing)。 (我想每个人都有免费的双关语),我正在尝试它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 2016-05-30
    • 2015-12-02
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多