I am using ngDialog with Angular and Typescript however I am trying to change to Angular-ui-boostrap modal as it looks cooler and easier to use.
我正在使用ngDialog与角和打字稿,但是我正在尝试更改为Angular-ui-boostrap模式,因为它看起来更酷,更容易使用。
I have a scenario that I show a list of records using e.g AddressListController and the user Clicks on a record e.g. an address then I show the Modal so the user can Edit/Delete the record. Alternatively user can select the add to add a new record that should show a modal. I want to delegate the Save / Update / Delete of the record (address) to the address controller e.g. AddressController.
我有一个场景,使用e显示一个记录列表。g AddressListController和用户点击一个记录,例如一个地址,然后我显示模式,以便用户可以编辑/删除记录。用户也可以选择add来添加一个新的记录,该记录应该显示一个模态。我想把记录(地址)的保存/更新/删除委托给地址控制器,例如:AddressController。
The problem that I have is that the modal is showing however the data is not populted. I am not sure if I am using the correct syntax in the template? Also, I prefer the controllerAs syntax.
我遇到的问题是,模态显示了数据没有被填充。我不确定我是否在模板中使用了正确的语法?另外,我更喜欢控制器的语法。
My code is as following:
我的代码如下:
In the list controller:
列表中的控制器:
selectAddress(index: number): void {
var address = this.contactAssocitedRecords.Addresses[index];
this.edit(address);
}
edit(item: Address) {
var promise = this.addressService.getAddressWithInfo(item.Id);
promise.then((response: ng.IHttpPromiseCallbackArg<AddressInfo>) => {
var addressInfo = response.data;
console.log(addressInfo); **// This returns all data**
var options: ng.ui.bootstrap.IModalSettings = {
templateUrl: "/js/controllers/_MaintainAddress.Html",
controller: 'CRM.Contacts.AddressController as addressCtrl',
windowClass: 'app-modal-window',
resolve: {
addressInfo: () => addressInfo
}
};
this.$uibModal.open(options).result
.then(updatedItem => this.savedAddress(updatedItem));
},
error => {
this.popupService.showError(error.data.message);
});
}
savedAddress(item: any): void {
console.log(item);
}
AddressController:
AddressController:
module CRM.Contacts {
export class AddressController {
private scope: ng.IScope;
static $inject = ["$http", "CRM.Contacts.ContactService", "CRM.Common.PopupService", "ngDialog",
'$uibModalInstance'];
constructor(private $http: ng.IHttpService, private contactService: ICrudService<Contact>,
private popupService: CRM.Common.IPopupService,
private ngDialog: angular.dialog.IDialogService,
private $uibModalInstance: ng.ui.bootstrap.IModalServiceInstance) {
}
save(addressToSave: Address) {
// TODO: do the actual saving
console.log(addressToSave);
this.$uibModalInstance.close(addressToSave);
}
}
angular.module("CRM").controller("CRM.Contacts.AddressController", AddressController);
}
}
Template:
模板:
<!-- Form Name -->
<legend>Edit Address...</legend>
<!-- Text input-->
<div class="form-group col-md-6">
<label class="col-md-4 control-label" for="Id">Id:</label>
<div class="col-md-8">
<input id="Id" name="Id" ng-disabled="true" type="text" ng-model="addressCtrl.addressInfo.Address.Id" placeholder="Id" class=" form-control">
</div>
</div>
<!-- Text input-->
<div class="form-group col-md-6">
<label class="col-md-4 control-label" for="streetNo">Street No:</label>
<div class="col-md-8">
<input id="streetNo" name="streetNo" type="text" ng-model="addressCtrl.addressInfo.Address.StreetNo" placeholder="Street No" class=" form-control" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group col-md-6">
<label class="col-md-4 control-label" for="streetName">Street Name:</label>
<div class="col-md-8">
<input id="streetName" ng-model="addressCtrl.addressInfo.Address.StreetName" name="streetName" type="text" placeholder="Street Name" class=" form-control" required="">
</div>
</div>
// removed for brevity
<div class="form-group col-md-offset-8">
<label class="col-md-4 control-label" for="save"></label>
<div class="col-md-8">
<button id="save" title="Save new record" data-toggle="tooltip" data-placement="right" name="submit"
ng-click="addressCtrl.save(addressCtrl.addressInfo.Address)" class="btn btn-success" ng-disabled="addNewAddress.$invalid">
<span class="glyphicon glyphicon-floppy-disk"></span>
</button>
</div>
</div>
</fieldset>
</form>
1 个解决方案
#1
3
In your AddressController you should inject the addressInfo and make it public so the template can access it. The data will come from the resolve function.
在您的AddressController中,您应该注入addressInfo并将其公开,以便模板能够访问它。数据将来自解析函数。
The AddressController
should like this:
AddressController应该是这样的:
module CRM.Contacts {
export class AddressController {
private scope:ng.IScope;
static $inject = ["$http", "CRM.Contacts.ContactService", "CRM.Common.PopupService", "ngDialog",
'$uibModalInstance', 'addressInfo'];
constructor(private $http:ng.IHttpService,
private contactService:ICrudService<Contact>,
private popupService:CRM.Common.IPopupService,
private ngDialog:angular.dialog.IDialogService,
private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
public addressInfo:any) {
}
save(addressToSave:Address) {
// TODO: do the actual saving
console.log(addressToSave);
this.$uibModalInstance.close(addressToSave);
}
}
angular.module("CRM").controller("CRM.Contacts.AddressController", AddressController);
}
#1
3
In your AddressController you should inject the addressInfo and make it public so the template can access it. The data will come from the resolve function.
在您的AddressController中,您应该注入addressInfo并将其公开,以便模板能够访问它。数据将来自解析函数。
The AddressController
should like this:
AddressController应该是这样的:
module CRM.Contacts {
export class AddressController {
private scope:ng.IScope;
static $inject = ["$http", "CRM.Contacts.ContactService", "CRM.Common.PopupService", "ngDialog",
'$uibModalInstance', 'addressInfo'];
constructor(private $http:ng.IHttpService,
private contactService:ICrudService<Contact>,
private popupService:CRM.Common.IPopupService,
private ngDialog:angular.dialog.IDialogService,
private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
public addressInfo:any) {
}
save(addressToSave:Address) {
// TODO: do the actual saving
console.log(addressToSave);
this.$uibModalInstance.close(addressToSave);
}
}
angular.module("CRM").controller("CRM.Contacts.AddressController", AddressController);
}