无法获取自定义指令属性值

时间:2015-09-16 02:08:08

标签: javascript angularjs

我有一个名为“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-Variablelink的值时,它会返回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>          

1 个答案:

答案 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);
}
相关问题