原文: https://stackoverflow.com/questions/31857735/using-controlleras-with-a-directive

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

With "controllerAs", the controller instance alias - vm, in your case - is published on the scope as the .vm property of the scope.

So, to access its properties (i.e. the properties of the controller), you need to specify {{vm.text}} or ng-click="vm.click".

When you use controllerAs syntax, then you have to use

bindToController: true

it will work in your directive.

When using 'controllerAs' syntax ,as above,the scope is bound to the controller’s 'this' reference. So it allows us to introduce a new namespace('vm' here) bound to our controller without the need to put scope properties in an additional object literal(say $scope). So accessing anything in controller's scope,requires 'vm' namespace, as,

'<button ng-click="click">{{vm.text}}</button>'

下面是另外一种风格的directive的写法:
<!DOCTYPE html>
<html ng-app="app"> <head>
<script data-require="angular.js@1.4.3" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head> <body>
<h1>Hello Plunker!</h1>
<test></test>
</body> </html>

  

angular
.module('app', []); angular
.module('app')
.directive('test', test); function test() {
return {
restrict: 'E',
template: '<button ng-click="vm.click()">{{vm.text}}</button>',
controller: testCtrlaaa,
controllerAs: 'vm'
}
}
// 下面的这三行可以不要的。
//angular
// .module('app')
// .controller('testCtrlttttada', testCtrlaaa); function testCtrlaaa() {
var vm = this;
vm.text = "TEST";
vm.click= function(){
alert('aaaa');
}
}

  



angular directive 的controllerAs的用法的更多相关文章

  1. angular directive scope

    angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...

  2. angular directive restrict 的用法

    E 表示该指令是一个element; A 表示该指令是attribute; C 表示该指令是class; M 表示该指令是注视 实例如下: 原帖:www.thinkster.io/angularjs/ ...

  3. angular directive

    1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式被声明: 取值有:E(元素),A(属性),C(类),M(注释),其中默认值为A: E(元素):<directiveName ...

  4. Angular JS中$timeout的用法及其与window.setTimeout的区别

    $timeout的用法 angular.js的$timeout指令对window.setTimeout做了一个封装,它的返回值是一个promise对象.当定义的时间到了以后,这个promise对象就会 ...

  5. angular directive指令内的参数

    angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...

  6. angular directive自定义指令

    先来看一下自定义指令的写法 app.directive('', ['', function(){ // Runs during compile return { // name: '', // pri ...

  7. AngularJS中Directive指令系列 - 基本用法

    参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...

  8. angular directive 深入理解

    由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In ...

  9. angular directive指令的复用

    “指令之之所以要定义成指令就是为了复用!” 指令一定是可以用在不同的controller里面的,为了在不同的controller去使用它,我们一定要给指定的配置项一个指令.这样才能跟外面的控制器进行交 ...

随机推荐

  1. 一次向svn中增加所有新增文件 svn add all new files【转】

    以下摘自:<卓有成效的程序员>之自动化 转自:http://blog.csdn.net/spare_h/article/details/6677435 我经常会一次往Subversion里 ...

  2. js数组高效去重

    http://blog.csdn.net/chengxuyuan20100425/article/details/8497277 这个方法的思路是先把数组排序,然后比较相邻的两个值. 排序的时候用的J ...

  3. Selenium2+python自动化53-unittest批量执行(discover)【转载】

    前言 我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到unittet里面的discover方法来加载用例了. 加载用例后,用unittest里面的Text ...

  4. Photoshop CC 2015

    1.Ctrl 加 + -号实现放大缩小2.Ctrl+T 自由变换3.Ctrl+D 取消选区,再选择处理完后想要选中别的区域的话要取消之前选中的区域,要不然之前的区域一直都是选中状态4.F12 将文件恢 ...

  5. Go嵌入类型及内部提升样例

    这个有点新鲜哟... package main import ( "fmt" ) type notifier interface { notify() } type user st ...

  6. debian 更换sh的默认链接为bash

    https://blog.csdn.net/mudongliangabcd/article/details/43458895

  7. 蓝牙遥控小车设计(三)——Amarino和 Android手机重力感应控制

    最近事真是多啊,一件接着一件的,加上自己拖延症~ - -! 遥控小车基本完成了,只是自己没时间来更新. 现在更新手机控制的部分 首先我们要熟悉一个软件—— 官网地址:http://www.amarin ...

  8. zoj2318

    zoj2318 题意 一个平面上给出很多圆,其中一个圆为现在自己的位置,问这个圆能不能冲出其它圆的包围(不能与其它圆相交). 分析 将所有圆心平移,使得自己的圆圆心处于原点,将所有圆半径增加自己圆的半 ...

  9. JDK/Java里的设计模式

    JDK/Java里的设计模式

  10. [BZOJ1143][CTSC2008]祭祀river(Dilworth定理+二分图匹配)

    题意:给你一张n个点的DAG,最大化选择的点数,是点之间两两不可达. 要从Dilworth定理说起. Dilworth定理是定义在偏序集上的,也可以从图论的角度解释.偏序集中两个元素能比较大小,则在图 ...