我正在使用v1.5组件,基本上(根据我的理解扩展)最佳实践指令的包装器。
有关1.5版本组件的更多信息,请访问:http://toddmotto.com/exploring-the-angular-1-5-component-method/
我有以下内容:
<span>Correclty showing: {{ data.type }}</span>
<book class="details" type="data.type"></book>
它的组件定义:
angular
.module('app')
.component('book', {
bindings: {
type: '='
},
template: function($element, $attrs) {
// Have tried A):
// console.log($attrs.type); // <- undefined
// And B):
$attrs.$observe('type', function(value) {
console.log(value); // <- undefined
});
// But.. C):
return "This works though {{ book.type }}"; // <- renders
}
});
A)
和B)
变体都会返回undefined
。 C)
呈现正确。
有没有办法在返回模板字符串之前访问模板函数中的属性?
答案 0 :(得分:13)
在1.5中,templateUrl现在根据文档https://docs.angularjs.org/guide/component获取注射剂。如果您使用ng-srict-di,除非您指定注射剂,否则您将收到错误....我正在使用ng-annotate来保存令人头疼的问题并保持代码清洁。这是一个例子(在TypeScript中):
angular.module('app').component('book', {
templateUrl: ['$element', '$attrs', function($element, $attrs) {
// access to the $element or $attrs injectables
}],
});
或在vanilla JS中没有ng-annotate:
<meta http-equiv="X-UA-Compatible" content="IE=edge" />