您的位置:首页技术文章
文章详情页

angular.js - 关于angular RouteProvider

【字号: 日期:2024-10-02 14:22:49浏览:51作者:猪猪

问题描述

config([’$routeProvider’, function($routeProvider){ $routeProvider.when} ]);config(function($routeProvider){ $routeProvider.when});请问这两种方式配置路由有什么区别?

问题解答

回答1:

http://www.html-js.com/article/2956

回答2:

首先看文档

angular.js - 关于angular RouteProvider

注意红色部分,如果不用显示指定参数的方式注入依赖,那么当你minify代码时,那些变量名可能被替换,从而导致运行时注入失败

回答3:

这两种都是依赖注入的方式,ng中的3种注入方式:a、推断式注入app.controller(’MyCtrl’, function($scope) { });

b、标注式注入var myFunc=function($scope) { });myFunc.$inject = [’$scope’];app.controller(’MyCtrl’,myFunc);

c、内联注入app.controller(’MyCtrl’, [’$scope’, function($scope) {}]);

第1种是根据写的参数名称,如$scope,内部自己调用$inject把$scope进行依赖注入,如果在前端开发中使用压缩工具,就会把$scope变成另外的字母了,就无法进行推断了,而另外两种方式你可以把function($scope)改成function(a)都没关系;第2种要多写一行代码;一般推荐使用第3种。

相关文章: