简体   繁体   中英

Trying to access a injected dependency outside controller method

Is there possible to access a injected dependency on controller outside on it?

 function clienteCreateController(ClientesService, recuperarEndereco) { var vm = this; vm.pesquisarCep = pesquisarCep; } function pesquisarCep(cep) { recuperarEndereco.find(cep) .success(function(data) { parseEndereco(data).bind(this); }) .error(function(err) { // showAlertDanger(vm, 'Cep inválido.'); console.log(err); }); } 

I'm calling the method from a button click.

Thanks

I do not think you can access the parameter recuperarEndereco in the outside function pesquisarCep . Because inside the execution context in function pesquisarCep , the variable is recuperarEndereco is not declared at all. Same as this JSFiddle: http://jsfiddle.net/dLhmozf3/ . It reports error:

function outsite () {
    console.log('param: ' + param);
}

var f = function (param) {
    var me = outsite;
    me();
};

f();

You need to define the outside function like: function pesquisarCep(cep, recuperarEndereco) { ... . And call it like pesquisarCep(cep, recuperarEndereco) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM