简体   繁体   English

AngularJS将变量传递给指令

[英]AngularJS pass variable to directive

I have to make some directive in an AngularJS app and I need to pass variables from my controller to the directives. 我必须在AngularJS应用中做出一些指令,并且需要将控制器中的变量传递给指令。

The problem is that I have to use a directive syntax who is not common for me, and I don't know where I have to declare my variable in the directive with this syntax. 问题是我必须使用对我来说不常见的指令语法,而且我不知道必须在使用该语法的指令中声明变量的位置。

Here is my directive : 这是我的指令:

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;

}

} }

Anyone have an idea ? 有人有主意吗?

Thanks. 谢谢。

// see this example of code to understand ho to use directives //请参阅此代码示例以了解如何使用指令

// 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM