Seb*_*hof 8 angularjs angularjs-directive angularjs-components
我正在为角度1.5风格的组件重构一些角度指令.
我的一些指令具有依赖于某个属性存在的行为,因此没有该属性具有特定的布尔值.使用我的指令,我使用链接功能完成此操作:
link: function(scope,elem,attrs, controller){
controller.sortable = attrs.hasOwnProperty('sortable');
},
Run Code Online (Sandbox Code Playgroud)
我如何使用角度1.5样式的组件语法?
我能做的一件事就是添加一个绑定,但是我需要指定一个布尔值.我想保持模板原样.
使用绑定而不是直接引用DOM属性:
angular.module('example').component('exampleComponent', {
bindings: {
sortable: '<'
},
controller: function() {
var vm = this;
var isSortable = vm.sortable;
},
templateUrl: 'your-template.html'
});
Run Code Online (Sandbox Code Playgroud)
模板:
<example-component sortable="true"></example-component>
Run Code Online (Sandbox Code Playgroud)
使用单向绑定(由'<'表示)控制器实例上的变量'sortable'的值(此处为视图模型命名为vm)如果设置如示例所示,则为布尔值true.如果您的可排序属性当前在模板中包含一个字符串,那么'@'绑定也可能是一个合适的选择.在这种情况下,vm.sortable的值将是一个字符串(如果在组件标记上未定义属性,则为undefined).
检查仅仅存在可排序属性的方式如下:
bindings: { sortable: '@' }
// within the controller:
var isSortable = vm.sortable !== undefined;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6284 次 |
| 最近记录: |