angularJs模版注入的两种方式

本文详细介绍了AngularJS中两种依赖注入的方式:声名式注入和直接注入。通过具体的代码示例,展示了如何在app.js、controller.js及config.js等文件中进行依赖注入,并配置路由。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,声名式注入

1:app.js:

var myApp = angular.module("myApp",["ngRoute"]);

2:controller.js:

myApp.controller("indexCtrl",['$scope',function($scope){              //如果想用混淆,必须使用中括号中声名注入参数

}])

3:config.js:

myApp.config(function($routeProvider, $locationProvider) {
    $routeProvider
    .when('/a', {
        templateUrl: 'web/templates/a.html',
        controller: 'aController'
    })
    .when('/b', {
        templateUrl: 'web/templates/b.html',
        controller: 'bController',
    .otherwise({
        redirectTo: '/a'
    });
});

二,直接注入

1:app.js

angular.moudle("myApp",[

  'ngRoute',

  'myApp.config',

      'myApp.controllers'

]);

 

2:controllers.js

angular.module("myApp.controllers",[])  //如果想用混淆,必须使用中括号中声名注入参数

.controller("myCtrl",function($scope){

})

.controller("otherCtrl",function($scope){

})

3:config.js

angular.module("myApp.config",[])

.config(function($routeProvider, $locationProvider) {
    $routeProvider
    .when('/a', {
        templateUrl: 'web/templates/a.html',
        controller: 'aController'
    })
    .when('/b', {
        templateUrl: 'web/templates/b.html',
        controller: 'bController',
    .otherwise({
        redirectTo: '/a'
    });
});

转载于:https://www.cnblogs.com/dasenglin/p/5529660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值