首页 文章

关于角度中的DI的一些问题

提问于
浏览
0

我正在练习DI并尝试在angular.js中进行模块化 . 我搜索了一些教程并尝试重新编码 .

我最初的计划如下(我可能有错误的概念,请帮助指出):

  • An ng-app:myapp;

带有工厂服务的“finance2”模块:“currencyConverter”;

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

带控制器的模块“ctrl1”:InvoiceController . 然后我将服务模块注入其中

angular.module('ctrl1',['finance2']).controller('InvoiceController', ['currencyConverter', '$scope',function(currencyConverter,$scope) {
  • 然后我将控制器模块注入应用程序
var app = angular.module("switchableGrid",['ctrl1']);

这是完整的代码,jsfiddle.net / c7fF3 / 1 /,

但没有任何事情发生,有人会给我一个暗示吗?非常感谢 .

3 回答

  • 0

    为了你的小提琴我改变 Framework and extensions 部分第二个下拉到"no-wrap in body",我看到没有记录异常 .

    此外,如果您使用控制器作为语法,您应该使用

    this.currencies = currencyConverter.currencies;

    代替

    $scope.currencies = currencyConverter.currencies;

  • 1

    你正在使用 ng-app="myapp" ,但你的应用程序实际上是一个名为 switchableGrid 的模块

    要么改变标记

    <body ng-app="switchableGrid">
    

    或将脚本更改为

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

    试试这种方式

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

相关问题