javascript - angularjs: Unable to Inject one module into another -
overview:
i have module contains set of classes (models). inject module use classes. please let me know i'd doing wrong.
error:
code:
<body> <div ng-app="testapp"> <div ng-controller="controller"></div> </div> </body> <script src="https://code.angularjs.org/1.2.8/angular.min.js"></script> <script> // module classes, inject next module var classes = angular.module('models', []); classes.factory('car', function() { return { honk : function() { alert('beep beep!'); } } }); // main module. var clienteditapp = angular.module("testapp", [ "models" ]); // adding controller clienteditapp.controller('controller', [ '$scope', 'models', function($scope, models) { var c = new models.car(); c.honk(); } ]); </script> </html>
1 - models factory module. don't inject modules. inject 'car'. module can have many factories, inject required , when needed.
2 - factory cannot instantiated new. need used directly. eg car.honk()
updated code
<script> // module classes, inject next module var classes = angular.module('models', []); classes.factory('car', function() { return { honk : function() { alert('beep beep!'); } } }); // main module. var clienteditapp = angular.module("testapp", [ "models" ]); // adding controller clienteditapp.controller('controller', [ '$scope', 'car', function($scope, car) { car.honk(); } ]); </script>
Comments
Post a Comment