$controller的使用

参考:https://stackoverflow.com/questions/27866620/can-someone-provide-a-use-case-for-the-controller-service-in-angularjs

You can create common functions which are to be executed on $scope into one controller may be named 'CommonCtrl'.

angular.module('app',[]).controller('CommonCtrl', ['$scope', function($scope){
var self = this;
$scope.stuff1 = function(){ } $scope.stuff2 = function(){ }
self.doCommonStuff = function(){
// common stuff here
$scope.stuff1();
$scope.stuff2(); };
return self;
}]);

And inject this controller in other controllers let say 'TestCtrl1' like

angular.module('app',[]).controller('TestCtrl1', ['$scope','$controller', function($scope, $controller){
var commonCtrl = $controller('CommonCtrl',{$scope: $scope}); // passing current scope to commmon controller
commonCtrl.doCommonStuff();
}]);

Here, the in second argument of $controller service, we are passing dependencies that are required by CommonCtrl. So the doCommonStuff method will use TestCtrl1 controller's scope.

-----------------------------------------------------------------------------

To mention one, it is useful in creating the target controller during unit testing.

Lets say you have a controller with signature .controller('MainCtrl', function($scope, serviceA){..}).

In testing,

// ...

beforeEach(inject(function ($rootScope, $controller, serviceA) {

  // assign injected values to test module variables
scope = $rootScope.$new();
service = serviceA // create the controller, by passing test module variables values as dependencies
$controller('MainCtrl', {'$scope': scope, 'serviceA': service});
})); it('test on controller', function() {
//...
});

angularJs中$controller的使用的更多相关文章

  1. AngularJS中$watch

    $watch在digest执行时,如果watch观察的value与上一次执行时不一样时,就会被触发.angularjs内部的watch实现了页面随model的及时更新.$watch 方法在用的时候主要 ...

  2. AngularJS 中 Controller 之间的通信

    用 Angular 进行开发,基本上都会遇到 Controller 之间通信的问题,本文对此进行一个总结. 在 Angular 中,Controller 之间通信的方式主要有三种: 1)作用域继承.利 ...

  3. angularjs 的controller的三种写法

    AngularJS 的controller其实就是一个方法,它有三种写法: 第一种: <pre name="code" class="javascript" ...

  4. Angularjs中controller的三种写法

    在Angular中,Directive.Service.Filter.Controller都是以工厂方法的方式给出,而工厂方法的参数名对应着该工厂方法依赖的Service.angularjs中cont ...

  5. Angularjs之controller 和filter(四)

    Controller组件(http://www.angularjs.cn/A00C) 在AngularJS中,控制器是一个Javascript函数(类型/类),用来增强除了根作用域以外的作用域实例的. ...

  6. 在AngularJS的controller外部直接获取$scope

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/5560843.html ...

  7. angularJS的controller之间如何正确的通信

    AngularJS中的controller是个函数,用来向视图的作用域($scope)添加额外的功能,我们用它来给作用域对象设置初始状态,并添加自定义行为. 当我们在创建新的控制器时,angularJ ...

  8. 两强相争,鹿死谁手 — JQuery中的Ajax与AngularJS中的$http

    一.JQuery与AngularJS 首先,先简单的了解一下JQuery与AngularJS.从源头上来说,两者都属于原生JS所封装成的库,两种为平行关系. 二.Ajax请求与数据遍历打印 这里是Aj ...

  9. angularjs 外部调用controller中的方法

    angular.element(document.querySelector('[ng-controller=mainCtrl]')).scope().viewGo('tab.VIPPay_Succe ...

  10. 【angularJS】Controller控制器

    1. 定义 控制器(Controller)在AngularJS中作用是增强视图(View),AngularJS控制器是一个构造方法,用来向视图(View)中添加额外功能. ng-controller指 ...

随机推荐

  1. bitnami redmine配置全过程

    常见问题: 我在自己的机器上面配置完毕以后,移植到另外一台机器上面,登陆页面总是在检查network,并且最后网络加载失败,不论我是用桥接还是NAT方式连接.登陆系统以后,我尝试连接网络失败,尝试执行 ...

  2. [EF Core]数据迁移(二)

    摘要 在实际项目中,大多都需要对业务逻辑以及操作数据库的逻辑进行分成操作,这个时候该如何进行数据的迁移呢? 步骤 上篇文章:EF Core数据迁移操作 比如,我们将数据上下文放在了Data层. 看一下 ...

  3. UITabBar 详解

    1.push时,将tabar隐藏,方法1,在push之前,加入如下代码: -(IBAction)btnOnClicked:(id)sender { SQVideoListViewController ...

  4. RDIFramework.NET V2.7 Web版本号升手风琴+树型文件夹(2级+)方法

    级+)"界面风格,以展示多级功能菜单,满足用户的要求.Web展示效果例如以下: 要以"手风琴+树型文件夹(2级+)"的风格来展示功能模块,我们须要在"系统配置& ...

  5. JavaScript:动态代理之远程代理

    背景 2008 第一次使用 AJAX 的时候好像使用的是 AJAX.NET,该组件支持为服务器 C# 类型提供 JS 代理,当时不是十分的明白.设计模式中有代理模式,不过真正需要我们手工写代理的次数却 ...

  6. PCM转MP3工具的封装

    PCM转MP3工具的封装 说明 1. 对 PCM 转 MP3 进行了简单的封装. 2. 使用 https://github.com/wuqiong/mp3lame-for-iOS 生成支持64位的 l ...

  7. 制作高仿QQ的聊天系统(上)—— 布局文件 & 减少过度绘制

    由于没有自己的服务器,我就找了个能实现双方通信的SDK,这个SDK是友盟的用户反馈SDK.本系列的博文关注的不是网络通信,而是如何在网络通信机制已经做好的情况下,做出一个可用的聊天系统.其实,刚开始做 ...

  8. C# 访问操作注册表整理

    一..net注册表操作简介 .net中Registry 类,RegistryKey 类提供了操作注册表的接口 RegistryValueKind,用于指定操作注册表的数据类型. 二.使用实例 //获取 ...

  9. [转]用chrome模拟微信浏览器访问需要OAuth2.0网页授权的页面

    FROM : http://blog.csdn.net/gavin_luo/article/details/40620217 现在很流行微信网页小游戏,用html5制作的小游戏移过来,可以放到微信浏览 ...

  10. 微信二维码扫描无法下载APK文件的解决办法