角度指令 - 更改模板内的数据
angular directive - change data inside the template
我正在开发一个演示仪表板,在该仪表板内有标题。我正在使用我的"标头"指令"生成"标头。(希望到目前为止我以正确的方式做到这一点)
应用.js
myapp.directive('header', function() {
return {
templateUrl: '../../partials/header.html'
};
});
标头.html:
<h1>Logo</h1>
<span>{{breadcrumbs}}</span>
partials/dashboard.html
<header breadcrumbs="home"></header>
我是否可以使用"痕迹导航"数据并将其传递到我加载的标题模板?
已尝试以下方法,但未成功:
myapp.directive('header', function() {
return {
breadcrumbs: 'for the sake of this example it can be static',
templateUrl: '../../partials/header.html'
};
});
存在两种实现目标的方法:
可以使用隔离作用域:
myapp.directive('header', function() {
return {
scope: {
breadcrumbs: "@"
},
templateUrl: '../../partials/header.html'
};
});
或link
函数attr
属性:
myapp.directive('header', function() {
return {
templateUrl: '../../partials/header.html',
link: function(scope, element, attrs){
scope.breadcrumbs = attrs.breadcrumbs
}
};
});
上级:
如果在属性 ( <header breadcrumbs="{{breadcrumbs}}"></header>
breadcrumbs
中使用插值 ,上面的代码可以如下所示:
隔离范围:
myapp.directive('header', function() {
return {
scope: {
breadcrumbs: "=" //two way binding
},
templateUrl: '../../partials/header.html'
};
});
link
函数的attr
属性:
myapp.directive('header', function() {
return {
templateUrl: '../../partials/header.html',
link: function(scope, element, attrs){
scope.breadcrumbs = scope.$eval(attrs.breadcrumbs) //eval interpolation value in this scope
}
};
});
是的,你可以这样做。
myapp.directive('header', function() {
return {
scope:{
breadcrumbs: '@'
},
templateUrl: '../../partials/header.html'
};
});
您可以直接在模板中使用breadcrumbs
。无需在链接功能内执行任何操作。
相关文章:
- AngularJs指令,该指令创建内部有数据对象的新指令
- 如何将角度输入指令数据传递给控制器
- 不适用于动态数据的Angular指令来自$http
- 对于使用传递的数据计算的局部范围变量,角度绑定在自定义指令中不起作用
- AngularJs 在指令中操作来自服务响应的数据
- 基于范围数据更改指令模板中的元素
- 指令中的数据未在ng重复中显示
- 如果指令包含在另一个指令中,我如何在隔离范围内添加双向数据绑定属性
- AngularJS nvd3折线图指令未在数据更改时重新绘制(非实时)
- AngularJS指令数据正在被覆盖
- 单元测试指令-无法强制使用假数据
- 控制器和外部指令之间的双向数据绑定
- AngularJS没有't解析指令中动态加载的数据
- Highcharts数据将不会使用角度指令加载
- 在指令初始化和加载数据后调用函数
- 当我从有界数据派生输出时,为什么我的自定义指令不更新
- 当uib分页被包装在另一个指令中时,AngularJS表数据没有更新
- 为什么数据在指令之外更改
- 在$invalid上添加验证指令并从文本框中删除数据的指令
- Angularjs如何将数据从控制器传递到指令