控制器外部的角度绑定数据
Angular bind data outside of controller
我有一个看起来像iPad应用程序的Angular应用程序。有一个带有按钮和下拉菜单的导航栏。导航栏在我的索引.html文件中声明。现在在我的不同页面中,我想向导航栏添加功能。现在我正在像这样向导航栏添加按钮
$rootScope.actionButtons.push({
icon: 'icon-history',
content: '<select ng-change="previewRevision()" ng-model="revision">'
<option value="">--Backlog--</option>
<option ng-repeat="old in journal.backlog" value="{{old.revision}}">Revision {{old.revision}}</option>
</select>',
title: 'Visa backlog'});
按下导航栏中的按钮时,此视图中的内容将在弹出框中打开。但是,由于用于呈现按钮的标记位于控制器之外,因此没有数据绑定到它,这使得它有点无用。
如何绑定当前范围之外的数据?
Angular 不知道你的内容。为了使其工作,我们需要使用$compile
服务编译此源代码。
所以让我们创建"编译器":
.directive( 'compileData', function ( $compile ) {
return {
scope: true,
link: function ( scope, element, attrs ) {
var elmnt;
attrs.$observe( 'template', function ( myTemplate ) {
if ( angular.isDefined( myTemplate ) ) {
// compile the provided template against the current scope
elmnt = $compile( myTemplate )( scope );
element.html(""); // dummy "clear"
element.append( elmnt );
}
});
}
};
});
将指令添加到 HTML 中,例如:
<div compile-data template="{{actionButtons.content}}"></div>
编译后,您将使用下面提到的 HTML 绑定您的范围
<select ng-change="previewRevision()" ng-model="revision">'
<option value="">--Backlog--</option>
<option ng-repeat="old in journal.backlog" value="{{old.revision}}">Revision {{old.revision}}</option>
</select>
希望会有所帮助
相关文章:
- 在Knockout中绑定数据后获取数据
- 使用Angular not Binding引导模式绑定数据
- FusionCharts MVC - JSON Result 未绑定数据
- 如何在 js / jquery 中绑定“数据追加”事件
- AngularJS如何绑定数据
- AngularJs的Kendo UI:当用户在网格中选择行时,如何在文本框字段上绑定数据
- angularjs:自定义直接中的绑定数据在使用“”后不再更新;track-by-;在ng重复中
- 访问表单控制AngularJS中的值,而不绑定数据
- 使用d3.js在绑定数据内数组(以恒定性动态生成d3.svg.line)
- 在 Angular 中的控制器之间绑定数据
- 在网格视图中显示不同的消息,用于没有绑定数据和未检索到结果
- kendoui 网格,当自动绑定配置设置为 false 时绑定数据
- 在 d3.js 中重新绑定数据的最佳方法
- 绑定数据在 d3.js 中不起作用
- 当本地绑定数据为整数时,对剑道 UI 网格进行筛选
- 控制器外部的角度绑定数据
- 在点击事件上获取绑定数据
- 使用下拉列表绑定数据
- 如何在 d3.js 中更新绑定数据
- 如何在 Handlebars.js 模板上绑定数据以进行自动值更新