gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:59:08 25 4
gpt4 key购买 nike

我有一个名为“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>

现在,当我尝试在指令的 link 函数中访问属性 transaction-Typestorage-Variable 的值时,它返回未定义。值可以通过 attrs.transactionType 访问,但不能通过 scope.sTransactionType 获取。

我试图更改属性名称、范围变量名称。

如何获取范围变量中的自定义指令属性值?

更新后的代码:

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>

最佳答案

你的属性命名是错误的。

全部小写

这样试试

<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);
}

关于javascript - 无法获取自定义指令属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com