user2837851
user2837851

Reputation: 1191

Some problems about DI in angular.js

I am practicing DI and tried to be modular in angular.js.I had searched some tutorial and try to recode it.

What I plan at first is as following(I might have the wrong concepts,please help to point out):

  1. An ng-app:myapp;

    An module "finance2" with an factory service:"currencyConverter";

    angular.module('finance2', []).factory('currencyConverter', function() {

    An module "ctrl1" with an controller:InvoiceController. Then I inject the service module into it

    angular.module('ctrl1',['finance2']).controller('InvoiceController', ['currencyConverter', '$scope',function(currencyConverter,$scope) {
  2. Then I inject the the controller module to the app
     var app = angular.module("switchableGrid",['ctrl1']);

Here is the complete code,jsfiddle.net/c7fF3/1/,

But nothing happend, could some one give me an hint?Many thanks.

Upvotes: 0

Views: 63

Answers (3)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

Try this way

angular.module('app', [])
    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider

            .when('/xxx', { templateUrl: 'app/xxx.html', controller: 'xxxCtrl' })

    }])

    .factory(
        'currencyConverter',
        function ($resource) {
            return $resource(URL);
        })


    .controller('xxxCtrl', ['$scope', '$http', '$routeParams', '$route', function ($scope, $http, $routeParams, $route) {      
$scope.currencies = currencyConverter.currencies;
    }])

Upvotes: 0

doodeec
doodeec

Reputation: 2927

you are using ng-app="myapp" but your app is actually a module called switchableGrid

either change markup to

<body ng-app="switchableGrid">

or change the script to

angular.module('myapp', ['ctrl1']);

Upvotes: 1

Chandermani
Chandermani

Reputation: 42669

For your fiddle i changed the Framework and extensions section second dropdown to "no-wrap in body" and i see not exception being logged.

Also if you are using controller as syntax, you should use

this.currencies = currencyConverter.currencies;

instead of

$scope.currencies = currencyConverter.currencies;

Upvotes: 0

Related Questions