Angular 1.5组件方法templateUrl+函数
Angular 1.5 component method templateUrl + function
我正在尝试让一个应用程序与angular 1.5.0-beta 2 一起工作
要做出"指令",我有以下代码:
myApp.component('gridshow', {
bindings: {
slides: '='
},
controller: function() {
},
controllerAs: 'grid',
template: function ($element, $attrs) {
// access to $element and $attrs
return [
'<div class="slidegrid">',
'<div ng-repeat="slide in grid.slides">',
'{{slide.image}}',
'</div>',
'</div>'
].join('')
}
});
我喜欢模板返回一个可以访问$element
和$attrs
的函数的想法,但我如何将其与templateUrl组合?
在1.5.0-beta 2中,templateUrl
可以是一个由注入器调用的函数。$element
和$attrs
被注入到component
中的template
和templateUrl
函数以及任何其他依赖项中。
这意味着
...
templateUrl: function ($element, $attrs) {
// access to $element and $attrs
...
return $attrs.uninterpolatedTemplateUrl;
}
可以取而代之。
我通过以下技术解决了这个问题。这可能会对你有所帮助。
模板
<div data-ng-repeat="field in $ctrl.fields track by $index">
<render-field data-field-type="{{field.type}}"></render-field>
</div>
组件
/**
* @ngdoc Component
* @name app.component.renderField
* @module app
*
* @description
* A component to render Field by type
*
* @author Mohan Singh ( gmail::mslogicmaster@gmail.com, skype :: mohan.singh42 )
*/
(function () {
'use strict';
angular
.module('app')
.component('renderField', {
bindings: {
fieldType: '@',
},
template: '<div ng-include="$ctrl.templateUrl">',
controller: [
function () {
var $ctrl = this;
$ctrl.$onInit = initialization;
$ctrl.$onDestroy = onDestroy;
$ctrl.$onChanges = onChanges;
/**
* public properties
*/
/**
* public methods
*/
/**
* @function
* @name initialization
* @description
* A component's lifeCycle hook which is called after all the controllers on an element have been constructed and had their bindings initialized
*/
function initialization() {
}
/**
* @function
* @name onChanges
* @description
* A component's lifeCycle hook which is called when bindings are updated.
*/
function onChanges(bindings) {
if(bindings.fieldType && bindings.fieldType.isFirstChange()){
//$ctrl.fieldType['text' | 'textarea' | 'select' | 'radio']
$ctrl.templateUrl = 'partials/fields/'+$ctrl.fieldType+'.html';
}
}
/**
* @function
* @name onDestroy
* @description
* A component's lifeCycle hook which is called when is called on a controller when its containing scope is destroyed.
* Usefull to release external resources, watches and event handlers.
*/
function onDestroy() { }
}]
});
})();
@estus解决方案一直适用于我,直到我改进了我的脚本。未经验证,出现以下错误:
Error: [$injector:unpr] Unknown provider: eProvider <- e
对我有效的解决方案是:
['$element', '$attrs', function($element, $attrs) {
return $attrs.uninterpolatedTemplateUrl;
}]
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- 如何从指令中的templateUrl运行$scope函数
- Angular 1.5组件方法templateUrl+函数
- 如何将服务注入AngularJS'的UI路由器templateUrl函数
- 带templateUrl函数的指令refresh/redraw/update
- angularjs,使用路由中的一个函数作为templateUrl
- 使用双向数据绑定的AngularJS指令templateUrl函数
- templateUrl 函数未使用显式注释,无法在严格模式下调用