【发布时间】:2016-06-04 05:11:53
【问题描述】:
对于我正在进行的一个项目,我们将一些重复的视图和代码重构到 Angular 1.5 组件中。
这些组件专门打开一个 Angular UI 模态,当模态关闭时,应该在最上面的范围内执行一些回调。我们遇到的问题是,当我们关闭 modal 时,我们设置为回调的函数没有被调用。
我确保咨询了Angular 1.5 Documentation for Components,但是关于“&”分类器如何工作的部分特别模糊。我引用:
输出通过 & 绑定实现,作为组件事件的回调函数。
我发现这一段几乎没用;因此,在这种情况下,我不知道我是否正确使用了“&”绑定分类器。以下是我正在做的事情:
MainFeature.html
<div ng-repeat="thing in vm.things">
<sub-feature onSave="vm.onSave()" thing="thing"></sub-feature>
</div>
MainFeatureCtrl.js
(function () {
'use strict';
angular.module('app').controller('mainFeatureCtrl', [
mainFeatureCtrl
]);
function mainFeatureCtrl() {
var vm = this;
vm.onSave = function () {
// Save stuff
console.log('I should see this but do not.');
};
}
})();
SubFeatureCmpt.js
(function () {
'use strict';
angular.module('app').component('subFeature', {
templateUrl: 'App/MainFeature/SubFeature/SubFeature.html',
controller: 'subFeatureCtrl',
controllerAs: 'vm',
bindings: {
thing: '=',
onSave: '&' // Point of contention - expression?
}
});
})();
SubFeature.html
<span ng-click="vm.edit()">{{vm.thing}}</span>
SubFeatureCtrl.js
(function () {
'use strict';
// subFeatureModalSvc is merely a service that lets me open
// an Angular UI modal. It is not necessary to include that
// code for the purposes of my question.
angular.module('app').controller('subFeatureCtrl', [
'subFeatureModalSvc', subFeatureCtrl
]);
function subFeatureCtrl(subFeatureModalSvc) {
var vm = this;
vm.edit = function () {
subFeatureModalSvc.edit(vm.thing)
.then(function () {
console.log('I do see this');
// This line is the problem line.
// I've verified that it's defined, and a function...
// The problem is it's not a callback to vm.onSave
// as defined up in mainFeature.js!
vm.onSave();
});
};
}
})();
此代码当前在“确认”模式对话框时产生以下输出:
I see this.
问题::双重的。首先,我是否正确使用了“&”绑定分类器来为我的子功能组件设置事件?其次,在目前的状态下(这个),它不工作 - 我不知道如何让它工作。我应该怎么做才能确保在关闭模式时看到以下输出:
I see this
I should see this but do not
【问题讨论】:
-
不应该是
onSave="vm.onSave()"吗? -
已经修复。不过很好。
标签: javascript angularjs components