1
從我的模板中,我使用ng-click="$widget.addItem()"
觸發$uibModal
。
一切正常,打開,關閉等
的問題是,我不能讓$modal.close($value)
回調值繼續工作。函數本身工作正常,模式關閉,並且調用promise成功函數。問題是,它總是返回undefined
。
組件控制器
app.component('list', {
templateUrl: 'widgets/list.html',
controllerAs: '$widget',
controller: function($uibModal) {
// Add item through modal
this.addItem = function() {
// Init and open $uibModal
var modalInstance = $uibModal.open({
component : 'modalAddItem',
size : 'md'
});
// $uibModal promise
modalInstance.result.then(function(params) {
console.log(params)
// ---> undefined
});
}
}
}
模態模板
<div class="modal-add-item">
<div class="modal-body" id="modal-body">
// Some template stuff here
</div>
<div class="modal-footer">
<button class="btn btn-sm btn-default" ng-click="$modal.ok()">Ok</button>
</div>
</div>
模態分量
app.component('modalAddItem', {
templateUrl: 'widgets/modals/add-item.html',
bindings: {
resolve: '<',
close: '&',
dismiss: '&'
},
controllerAs: '$modal',
controller: function ($scope) {
this.ok = function() {
this.close({item: 'picked item'});
}
}
});
一切工作正常。但無論我嘗試,關閉功能總是返回undefined
哇哦,你從字面上必須使用'$ value'爲重點,我認爲這是僅用於演示目的。非常感謝! –
不受歡迎:)實際上,在angular中,&bind期望調用者傳遞一個映射作爲參數,其中鍵必須與原始鍵匹配,在這種情況下,它是建立鍵的角度。看看如何&綁定工作深入https://stackoverflow.com/questions/43378614/javascript-objects-as-function-arguments-in-angular-directive-properties/43379581#43379581 – Karim
啊,我已經想知道'&'綁定做了什麼。感謝您的解釋。 –