angularjs+requirejs动态调用controller的问题
根据这篇文章修改了原本写的一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那我再来最后一次修正:
indexCtrl.js
改为: