gpt4 book ai didi

javascript - AngularJS:延迟加载 Controller 和内容

转载 作者:行者123 更新时间:2023-12-03 21:37:41 25 4
gpt4 key购买 nike

在这个简化的场景中,我有两个文件:index.htm、lazy.htm。

index.htm:

var myApp = angular.module('myApp', []);
myApp.controller('embed',function($scope){
$scope.embed = 'Embedded Controller';
});
<div ng-controller="embed">{{embed}}</div>
<div ng-include="'lazy.htm'"></div>

懒惰.htm

myApp.controller('lazy',function($scope){
$scope.lazy = 'Lazy Controller';
});
<div ng-controller="lazy">
{{lazy}}
</div>

结果是一个错误:“参数‘lazy’不是一个函数,未定义”

使用函数代替

懒惰.htm

function lazy($scope) {
$scope.lazy = 'Lazy Controller';
}
<div ng-controller="lazy">
{{lazy}}
</div>

这一直有效到版本 1.3 beta 14。在 beta 15 中删除了全局 Controller 功能:https://github.com/angular/angular.js/issues/8296

那么现在,动态获取 Angular lazy.htm内容的更好方法是什么?

更新:

在这篇文章(http://ify.io/lazy-loading-in-angularjs)中我找到了另一种可能的解决方案。 $controllerProvider 允许我们在 Angular 引导后注册新的 Controller 。奇迹般有效。在 v1.3.0-beta.18

中测试

index.htm:

var myApp = angular.module('myApp', [])
.controller('embed',function($scope){
$scope.embed = 'Embedded Controller';
})
.config(function($controllerProvider) {
myApp.cp = $controllerProvider;
});

<div ng-controller="embed">{{embed}}</div>
<div ng-include="'lazy.htm'"></div>

懒惰.htm

myApp.cp.register('lazy',function($scope){
$scope.lazy = 'Lazy Controller';
});
<div ng-controller="lazy">
{{lazy}}
</div>

更新2:

另外两个可行的替代方案是:

懒惰.htm

_app = $('[ng-app]').scope();    
_app.lazy = function($scope) {
$scope.lazy = 'Lazy Controller';
};

或者

var $rootScope = $('[ng-app]').injector().get('$rootScope');        
$rootScope.lazy = function($scope) {
$scope.lazy = 'Lazy Controller';
};

但我相信最后两个示例不应该在生产中使用。

最佳答案

您还可以使用 jquery 来解析 $routeProvider

app.js

/* Module Creation */
var app = angular.module ('app', ['ngRoute']);

app.config(['$routeProvider', '$controllerProvider', function($routeProvider, $controllerProvider){

/*Creating a more synthesized form of service of $ controllerProvider.register*/
app.registerCtrl = $controllerProvider.register;

function loadScript(path) {
var result = $.Deferred(),
script = document.createElement("script");
script.async = "async";
script.type = "text/javascript";
script.src = path;
script.onload = script.onreadystatechange = function (_, isAbort) {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
if (isAbort)
result.reject();
else
result.resolve();
}
};
script.onerror = function () { result.reject(); };
document.querySelector("head").appendChild(script);
return result.promise();
}

function loader(arrayName){

return {
load: function($q){
var deferred = $q.defer(),
map = arrayName.map(function(name) {
return loadScript('js/controllers/'+name+".js");
});

$q.all(map).then(function(r){
deferred.resolve();
});

return deferred.promise;
}
};
}

$routeProvider
.when('/', {
templateUrl: 'views/foo.html',
resolve: loader(['foo'])
})
.when('/bar',{
templateUrl: 'views/bar.html',
controller: 'BarCtrl',
resolve: loader(['bar'])
})
.otherwise({
redirectTo: document.location.pathname
});
}]);

/views/foo.html

<section ng-controller='FooCtrl'>
{{text}}
</section>

js/controllers/foo.js

/*Here we use the synthesized version of $controllerProvider.register 
to register the controller in view*/
app.registerCtrl('FooCtrl',function($scope){
$scope.text = 'Test';
});

/views/bar.html

<section>
{{text2}}
</section>

js/controllers/bar.js

app.registerCtrl('BarCtrl',function($scope){
$scope.text2 = 'Test';
});

关于javascript - AngularJS:延迟加载 Controller 和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25168593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com