我正在尝试ng-repeat一个自定义指令,该指令具有应在迭代过程中更改的属性。
这是我的html:
<div ng-controller="WalletsController as controller">
<bitcoin-address ng-repeat="bitcoin_label in controller.getWallets()" bitcoin-label="bitcoin_label"></bitcoin-address>
</div>
这是我的控制器:
(function() {
var app = angular.module('wallets', [ ]);
app.controller(
"WalletsController",
function($scope, $http) {
this.wallets = [];
var controller = this;
this.getWallets = function() {
return controller.wallets;
};
$http.get("wallet_addresses").success(
function(data) {
for (var i = 0; i < data.length; i++) {
var curWallet = data[i];
$scope[curWallet.label] = {
label: curWallet.label,
address: curWallet.address,
balance: curWallet.balance
};
controller.wallets.push(curWallet.label);
}
}
);
});
app.directive(
'bitcoinAddress',
function() {
return {
restrict: 'E',
templateUrl: '../../resources/html/bitcoin-address.html',
scope: {
bitcoinLabel: '=',
}
};
}
);
})();
这是我的模板:
<div class="col-md-8 dashboardAddressCell dropdown-toggle" data-toggle="dropdown">{{bitcoinLabel.address}}</div>
发生的情况是模板无法解析bitcoinLabel变量。我尝试指定一个常量值,并且该模板有效。我的结论是我没有在html部分中正确指定bitcoin_label属性。我也尝试过使用{{bitcoin_address}},但是angularjs对此有所抱怨。
我也尝试使用以下html代码:
<div ng-controller="WalletsController as controller">
<!-- <tr><th>Indirizzo</th><th>Saldo</th><th></th>-->
<div ng-repeat="bitcoin_label in controller.getWallets()">
{{bitcoin_label}}
<bitcoin-address bitcoin-label="bitcoin_label"></bitcoin-address>
</div>
<bitcoin-address bitcoin-label="ciccio"></bitcoin-address>
</div>
它也不起作用,但是至少它显示{{bitcoin_label}}值。
这个问题似乎很简单。代替
controller.wallets.push(curWallet.label);
您应该推送相应的$scope[curWallet.label]
对象:
controller.wallets.push($scope[curWallet.label]);
因为curWallet.label
只是一个字符串,所以在第一种情况下wallets
以字符串数组结尾。然而,你需要wallets
成为对象的数组,每一个具有address
,label
,balance
性质。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句