angular directive 的controllerAs的用法
原文: 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的用法的更多相关文章
- angular directive scope
angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...
- angular directive restrict 的用法
E 表示该指令是一个element; A 表示该指令是attribute; C 表示该指令是class; M 表示该指令是注视 实例如下: 原帖:www.thinkster.io/angularjs/ ...
- angular directive
1.restrict (字符串)可选参数,指明指令在DOM里面以什么形式被声明: 取值有:E(元素),A(属性),C(类),M(注释),其中默认值为A: E(元素):<directiveName ...
- Angular JS中$timeout的用法及其与window.setTimeout的区别
$timeout的用法 angular.js的$timeout指令对window.setTimeout做了一个封装,它的返回值是一个promise对象.当定义的时间到了以后,这个promise对象就会 ...
- angular directive指令内的参数
angular.module('myApp', []) .directive('myDirective', function() { return { restrict: String, priori ...
- angular directive自定义指令
先来看一下自定义指令的写法 app.directive('', ['', function(){ // Runs during compile return { // name: '', // pri ...
- AngularJS中Directive指令系列 - 基本用法
参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...
- angular directive 深入理解
由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In ...
- angular directive指令的复用
“指令之之所以要定义成指令就是为了复用!” 指令一定是可以用在不同的controller里面的,为了在不同的controller去使用它,我们一定要给指定的配置项一个指令.这样才能跟外面的控制器进行交 ...
随机推荐
- kuangbin 带你飞 概率期望
正推不行就逆推! 经典问题:生日悖论 换成其互斥事件:m个人, 每个人生日都不相同的概率 ≤ 0.5 时最小人数. 这就是邮票收集问题的变形:每个邮票至少出现一次的概率 小于等于 0.5 邮票收集问题 ...
- zlib库剖析(1):实现概览
zlib库剖析(1):实现概览 http://blog.csdn.net/zhoudaxia/article/details/8034606 http://blog.chinaunix.net/uid ...
- ReadOnly与Enabled
txtDlrCode.ReadOnly = true; 1.当设置为只读,文本框有点击事件,点击该文本框还是可以响应点击事件 2.设置为只读,C#后台无法取得文本框的值,txtDlrCode.Text ...
- 只运行一个exe应用程序的使用案例
应用程序的exe启动设置 using System;using System.Diagnostics;using System.Reflection;using System.Runtime.Inte ...
- 原创 gif png bmp jeg 显示方法
/// <summary> /// 注意不要忘记引用那几个图片单元哦,除了bmp格式不需要引用任何单元, /// 其它图片格式都需要引用对应的图片单元 /// png ---> Vc ...
- Selenium2+python自动化74-jquery定位【转载】
转至博客:上海-悠悠 前言 元素定位可以说是学自动化的小伙伴遇到的一道门槛,学会了定位也就打通了任督二脉,前面分享过selenium的18般武艺,再加上五种js的定位大法. 这些还不够的话,今天再分享 ...
- docker从零开始(二)容器初体验
使用定义容器 Dockerfile Dockerfile定义容器内所需要的环境.对网络接口和磁盘驱动器等资源的访问在此环境中进行虚拟化,该环境与系统的其他部分隔离,因此您需要将端口映射到外部世界,并具 ...
- ros 如何使用 openni2_launch
There is very little actual code/etc in openni2_launch, it is mostly a thin wrapper around openni2_c ...
- mimikatz-域密码获取神器
mimikatz是一个法国人写的轻量级调试器.出众之处在于其可以直接从 lsass.exe 里猎取windows处于active状态账号明文密码,非常强大. 在网上找了一些相关的文章自己的一点总结吧 ...
- 火柴棒等式c++
先建立一个sum数组,打表存入1—9每个数字需要的火柴棒数,然后手动二重循环0—1000的所有数字,写一个int型函数用来计算每个数字需要多少根火柴棒(当前数字%10后在sum数组的下标),然后,最后 ...