我有一个名为“mycomponent”的自定义指令。
restrict: 'AE',
templateUrl: 'template.html',
scope:{
sTransactionType: '=transactionType',
sStorageVariable: '=storageVariable'
},
link: function(scope, element, attrs){
console.log("attrs.transactionType:: "+attrs.transactionType);
console.log("scope.sTransactionType:: "+scope.sTransactionType);
标记为:
<my-component transaction-Type="FundTransfer" storage-Variable="FundTransferToday"></my-component>
现在,当我尝试在transaction-Type
指令函数中访问属性storage-Variable
和link
的值时,它会返回undefined
。
可以通过attrs.transactionType
访问值,但无法通过scope.sTransactionType
获取。
我尝试更改属性名称,范围varibale名称。
如何在范围变量中获取自定义指令属性值?
更新代码:
var fundtransfer = angular.module("fundtransfer",['mydirectives']);
var controllers = {};
controllers.cntFundTransfer = function($scope, $rootScope){
}
var mydirectives = angular.module("mydirectives",['Constants']);
mydirectives.directive('myComponent', function($rootScope){
restrict: 'AE',
templateUrl: 'template.html',
scope:{
sTransactionType: '=transactionType',
sStorageVariable: '=storageVariable'
},
link: function(scope, element, attrs){
console.log("scope.sTransactionType:: "+scope.sTransactionType);
}
}
<my-component transaction-type="FundTransfer" storage-variable="FundTransferToday"></my-component>
答案 0 :(得分:4)
您的属性命名错误。
全部应为小写
试试这个
<my-component transaction-type="FundTransfer" storage-variable="FundTransferToday"></my-component>
<强> JS 强>
restrict: 'AE',
templateUrl: 'template.html',
scope:{
sTransactionType: '=transactionType',
sStorageVariable: '=storageVariable'
},
link: function(scope, element, attrs){
console.log("scope.sTransactionType:: "+scope.sTransactionType);
}