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):

  • 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) {
  • 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.


    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;


    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']);
    

    试试这种方式

    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;
        }])
    
    链接地址: http://www.djcxy.com/p/13630.html

    上一篇: 仍然对angularjs服务和工厂感到困惑

    下一篇: 有关angular.js中DI的一些问题