Inject a factory in a controller in angular

I´m trying to inject a factory in a controller in Angular, but I can not do. It is my code.

app.controller('ManejadorEventosVista', ['AdministradorMarcador', ManejadorEventosVista]);

'app' is the variable that corresponds to the module with their respective dependencies. The controller is 'ManejadorEventosVista' and requires the services provided by the factory 'AdministradorMarcador'.

function ManejadorEventosVista(){}

but when I want to use the factory 'AdministradorMarcador' in this part of the code , the factory is not recognized.

ManejadorEventosVista.prototype.seleccionarMarcadorOrigen = function (){ AdministradorMarcador.setTipoMarcador(AdministradorMarcador.MARCADOR_ORIGEN); };

How I can do to use the factory 'AdministradorMarcador' in ManejadorEventosVista.prototype.seleccionarMarcadorOrigen??..

Help or example to guide me??..Thanks..


ManejadorEventosVista needs to take an argument and you will be able to reference the AdministradorMarcador inside the function as whatever you named the first variable. Like so

function ManejadorEventosVista(AdministradorMarcador){/**your code here**/}

What you are doing with the line fragment ['AdministradorMarcador', ManejadorEventosVista] is declaring that your function depends on AdministradorMarcador , but without providing an argument to ManejadorEventosVista , AngularJS doesn't know how you intend to reference AdministradorMarcador inside your controller.

This is done in order to allow AngularJS scripts to be minified, especially by already existing solutions, as they would change the variables your function takes to single letter names, making it impossible for AngularJS to determine which service or factory to inject. Annotation uses strings and position-based ordering to allow your script to work, even after being minified since strings won't be altered by the process.

See also Latest Stable docs on Annotation

链接地址: http://www.djcxy.com/p/13640.html

上一篇: AngularJS $ http和$资源

下一篇: 以角度在控制器中注入工厂