angularjs+requirejs动态调用controller的问题

发布于 2022-09-02 20:54:43 字数 4147 浏览 13 评论 0

根据这篇文章修改了原本写的一个demo出现问题
app.js代码

define(['angular','routeConfig'], function (angular,routeConfig) {

    var app = angular.module('bookApp', ['ngRoute','ngSanitize'], function ($compileProvider, $controllerProvider) {
        routeConfig.setCompileProvider($compileProvider);
        routeConfig.setControllerProvider($controllerProvider);
});
     
    return app;

});

route-config.js代码:

define([], function () {

    var $controllerProvider,
        $compileProvider;

    function setControllerProvider(value) {
        $controllerProvider = value;
    }

    function setCompileProvider(value) {
        $compileProvider = value;
    }

    function config(templateUrl, controllerName) {
        if (!$controllerProvider) {
            throw new Error("$controllerProvider is not set!");
        }

        var defer,
            routeDefinition = {};

        routeDefinition.templateUrl = templateUrl;
        routeDefinition.controller = controllerName;
        routeDefinition.resolve = {
            delay:function ($q, $rootScope) {
                defer = $q.defer();
                    var dependencies = [controllerName];
                    require(dependencies, function () {
                        var controller = arguments[0],
                            template = arguments[1];
                        **//$controllerProvider.register(controllerName, controller);**
                        defer.resolve();
                        $rootScope.$apply()
                    })
                return defer.promise;
            }
        }
        return routeDefinition;
    }

    return {
        setControllerProvider:setControllerProvider,
        setCompileProvider:setCompileProvider,
        config:config
    }
})

route.js代码:

define(['app','routeConfig'], function(app,routeConfig){
  
   return app.config(['$routeProvider',function($routeProvider) {
            $routeProvider
              .when('/', routeConfig.config('views/index.html','indexCtrl'))
              .otherwise({ redirectTo: '/' });

              //$locationProvider.html5Mode(true).hashPrefix('!');

   }])
   
  
})

当indexCtrl.js为

define(['app','userBar','bookList','categoryTree'], function(app){
return app.controller('indexCtrl', ['$scope','$rootScope','$http','$location','$q', function ($scope,$rootScope,$http,$location,$q) {
            //alert($location.search().aaa);
            $rootScope.headTitle= "书城首页";
            //console.log($location);
            $scope.currentpage=$location.search().page?$location.search().page:1;
        }])
})

结果报错
Error: [ng:areq] http://errors.angularjs.org/1.3.4/ng/areq?p0=indexCtrl&p1=not%20aNaNunction%2C%20got%Object
当然,我把route-config.js中

**//$controllerProvider.register(controllerName, controller);**

注释去掉,再把indexCtrl.js中间部分改为

function indexCtrl($scope,$rootScope,$http,$location,$q,test){
            $rootScope.headTitle= "书城首页";
            //console.log($location);
            $scope.currentpage=$location.search().page?$location.search().page:1;
            //test.aaa=1;
        }
        return indexCtrl;

是可以的,但是又出现了所有directive指令失效的问题
指令categoryTree.js代码:

define(['app','CategoryFactory'], function (app,CategoryFactory) {
    app.directive('categoryTree', ['CategoryFactory',function (CategoryFactory) {
    return {
      restrict: 'A',
      replace: true,
      transclude: true,
      templateUrl: "js/views/categoryTree.html",
      scope: false,
      link:function($scope, element, attrs){
            CategoryFactory.getcategorytree()
                    .then(function(re) {
                    if(re.error==0){
                        $scope.categorys=re.category;
                    }
            });
      }            
    }
  }])
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掩饰不了的爱 2022-09-09 20:54:43

那我再来最后一次修正:

define(['app', 'routeConfig'], function(app, routeConfig) {

    return app.config(['$routeProvider', '$controllerProvider','$compileProvider','$filterProvider', '$provide', function($routeProvider,$controllerProvider,$compileProvider, $filterProvider, $provide) {
        app.register =
            {
                controller: $controllerProvider.register,
                directive: $compileProvider.directive,
                filter: $filterProvider.register,
                factory: $provide.factory,
                service: $provide.service
            };
            
        $routeProvider
            .when('/', routeConfig.config('views/index.html', 'indexCtrl'))
            .otherwise({redirectTo: '/'});

    }]);
});

indexCtrl.js改为:

define(['app','userBar','bookList','categoryTree'], function(app){ app.register.controller('indexCtrl', ['$scope','$rootScope','$http','$location','$q', function ($scope,$rootScope,$http,$location,$q) {
            //alert($location.search().aaa);
            $rootScope.headTitle= "书城首页";
            //console.log($location);
            $scope.currentpage=$location.search().page?$location.search().page:1;
        }])
})

再不行,我就没辙了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文