指令链接中的绑定不起作用
Binding in directive linking not working
我正在尝试制作一个简单的指令,它只返回大写文本。文本是通过绑定从控制器传递的。
基本思想是:
<div ng-controller="MainCtrl as main">
<div uppercase>{{main.message}}</div>
</div>
我的主要JS文件如下:
angular.module('app', [])
.controller('MainCtrl', function(){
this.message = 'hello world'
})
.directive('uppercase', function() {
return {
restrict: 'A',
transclude: true,
template: '<span ng-transclude></span>',
link: function(scope, el, attr) {
el.text() = el.text().toUpperCase();
}
}
})
我的茉莉花测试如下:
describe("App", function() {
var $compile
, $rootScope
, $controller
, MainCtrl
, element;
beforeEach(module('app'))
beforeEach(inject(function(_$compile_, _$rootScope_, _$controller_){
$compile = _$compile_;
$rootScope = _$rootScope_;
$controller = _$controller_;
MainCtrl = $controller('MainCtrl');
}))
describe("MainCtrl", function() {
it("should have a message property", function() {
expect(MainCtrl.message).toBe('hello world');
});
});
describe("Uppercase directive", function() {
it("should return uppercase text", function() {
element = '<p superman>{{MainCtrl.message}}</p>';
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('HELLO WORLD');
});
});
});
使用$timeout
等待Angular绑定完成,这就是使用.text()
:的方式
.directive('uppercase', function($timeout) {
return {
restrict: 'A',
transclude: true,
template: '<span ng-transclude></span>',
link: function(scope, el, attr) {
$timeout(function(){
el.text(el.text().toUpperCase());
});
}
}
});
Fiddle
相关文章:
- 对象文字方法上的Javascript绑定不起作用
- Knockout输入绑定不起作用
- 角度指令双向绑定不起作用
- 指令中选择输入的双向绑定不起作用
- Angularjs:ui路由嵌套状态下的绑定不起作用
- 为什么我的淘汰选项绑定不起作用
- jQuery Ajax 元素的后期绑定不起作用
- 样式绑定不起作用
- 在内联编辑的情况下,Knockout绑定不起作用
- 指令链接中的绑定不起作用
- Angular 1.5组件双向绑定不起作用
- 点击处理程序多次触发,解除绑定不起作用
- KnockoutJS - 引导程序 3 模式绑定不起作用
- foreach:绑定不起作用 ko.mapping.fromJS 数据
- 虚拟元素的挖空绑定不起作用
- 委托的“模板”绑定不起作用
- Angularjs 双向数据绑定不起作用;$watch也不起作用
- 使用 D3js 时,带有 DOM 的 AngularJS 数据绑定不起作用
- 视频播放事件中的 Angular 2 路绑定不起作用
- 如果语句和挖空中的可见绑定不起作用