angularjs - AngularJS将变量传递给指令
问题描述
我必须在 AngularJS 应用程序中创建一些指令,并且我需要将变量从我的控制器传递给指令。
问题是我必须使用一种对我来说不常见的指令语法,而且我不知道我必须在指令中用这种语法在哪里声明我的变量。
这是我的指令:
angular
.module('thermofluor')
.directive('tablePlate', tablePlate)
tablePlate.$inject = ['$timeout', '$q'];
function tablePlate( $timeout, $q )
{
var directive = {
link: link,
restrict: 'E',
template: '<h1>TEST</h1>'
};
return directive;
function link(scope, element ) {
return;
}
}
有人有想法吗?
谢谢。
解决方案
// 查看此代码示例以了解如何使用指令
// directive example
angular.module('thermofluor').directive('tablePlate', function(){
return {
restrict: 'E',
scope: {
arrayOfChecks: '='
},
template: '<h1>TEST</h1>' + htmlTable,
link: function(scope, element, attrs, ctrl){
}
}
});
var htmlTable =
'<table>' +
' <tr>' +
' <th>Name</th>' +
' <th>Value</th>' +
' <th>Active</th>' +
' </tr>' +
' <tr ng-repeat="c in arrayOfChecks">' +
' <td>{{c.name}}</td>' +
' <td>{{c.value</td>' +
' <td><input type="checkbox" ng-model="c.active"></td>' +
' </tr>' +
'</table>';
// controller
angular.module('thermofluor').controller('myController', function(){
$scope.myListOfChecks = [{name: 'A', value:'A value', active: false},
{name: 'B', value:'B value', active: true},
{name: 'C', value:'C value', active: false},
{name: 'D', value:'D value', active: true}];
});
// html
<div class="row" ng-controller="myController">
<table-plate array-of-checks="myListOfChecks"></table-plate>
</div>
推荐阅读
- c++ - 编译器忽略运算符新分配
- javascript - 在页面加载时显示底部而无需任何滚动
- java - 扫描仪 noSuchElementException。它的原因是什么?我该如何解决?
- node.js - 如何进行对话流会话
- php - 计划队列作业对我不起作用。我找不到任何解决方案
- handlebars.js - 车把模拟开关(案例)
- javascript - 扩展错误:违反以下内容安全策略指令:“script-src 'self' 'unsafe-eval'”
- xml - 如何将非标准字符转义为 Windows 1252 字符集
- java - ExoPlayer - java.lang.IllegalStateException:向死线程上的处理程序发送消息
- outlook-web-addins - 有没有办法通过单击上下文加载项中的内容来触发侧边栏?