wta*_*iro 40 javascript angularjs angular-ui-bootstrap
我想在ui.bootstrap.modal中使用angular组件.角度版本是1.5.
我尝试使用如下.
零件
function MyComponentController($uibModalInstance){
var ctrl = this;
ctrl.doSomething = function() {
//doSomething
}
}
app.component('myComponent', {
contoller: MyComponentController,
templateUrl: '/path/to/myComponent.html'
}
Run Code Online (Sandbox Code Playgroud)
父控制器
function parentController($uibModal){
var ctrl = this;
ctrl.openModal = function(){
var modalInstance = $uibModal.open({
template: '<my-component></my-component>'
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行时parentController.openModal()
,我得到了$ injector:unpr未知提供程序的错误,虽然模态窗口是打开的.
有没有办法在ui.bootstrap.modal中使用角度组件?如果您需要更多信息,请告诉我.
谢谢.
编辑
我有一种方法可以使用来自Renato Machado的ui.bootstrap.modal组件,感谢Renato.
但我觉得它有点复杂,不方便.所以最后我认为在模态中使用组件会更好.
Modal以常规方式打开(只需设置控制器和模板$uibModal.open({})
),模态包含具有您想要常用的逻辑的组件.
模态应该只有与模态相关的简单逻辑,如关闭模态窗口.
另一个主要与业务/应用程序相关的逻辑应该在组件中.
它很容易共同化.
PCa*_*che 63
编辑:从UI Bootstrap 2.1.0开始,对bootstrap模式中的组件有本机支持.看起来在2.1.0之后有几个快速版本来修复模态的一些问题,所以我一定会抓住最新版本.
有关使用UI Bootstrap 2.1.0+的版本,请参阅此Plunk
http://plnkr.co/edit/jy8WHfJLnMMldMQRj1tf?p=preview
angular.module('app', ['ngAnimate', 'ui.bootstrap']);
angular.module('app')
.component('myContent', {
template: 'I am content! <button type="button" class="btn btn-default" ng-click="$ctrl.open()">Open Modal</button>',
controller: function($uibModal) {
$ctrl = this;
$ctrl.dataForModal = {
name: 'NameToEdit',
value: 'ValueToEdit'
}
$ctrl.open = function() {
$uibModal.open({
component: "myModal",
resolve: {
modalData: function() {
return $ctrl.dataForModal;
}
}
}).result.then(function(result) {
console.info("I was closed, so do what I need to do myContent's controller now. Result was->");
console.info(result);
}, function(reason) {
console.info("I was dimissed, so do what I need to do myContent's controller now. Reason was->" + reason);
});
};
}
});
angular.module('app')
.component('myModal', {
template: `<div class="modal-body"><div>{{$ctrl.greeting}}</div>
<label>Name To Edit</label> <input ng-model="$ctrl.modalData.name"><br>
<label>Value To Edit</label> <input ng-model="$ctrl.modalData.value"><br>
<button class="btn btn-warning" type="button" ng-click="$ctrl.handleClose()">Close Modal</button>
<button class="btn btn-warning" type="button" ng-click="$ctrl.handleDismiss()">Dimiss Modal</button>
</div>`,
bindings: {
modalInstance: "<",
resolve: "<"
},
controller: [function() {
var $ctrl = this;
$ctrl.$onInit = function() {
$ctrl.modalData = $ctrl.resolve.modalData;
}
$ctrl.handleClose = function() {
console.info("in handle close");
$ctrl.modalInstance.close($ctrl.modalData);
};
$ctrl.handleDismiss = function() {
console.info("in handle dismiss");
$ctrl.modalInstance.dismiss("cancel");
};
}]
});
Run Code Online (Sandbox Code Playgroud)
原答案如下:
前几天我也试图解决这个问题.我把这个帖子中的信息和这个链接一起用来尝试提出另一种方法来实现这个目的.这些是我发现帮助我的一些参考链接:
https://github.com/angular-ui/bootstrap/issues/5683
http://www.codelord.net/(这个有助于理解传递参数到组件中的回调)
这里还有一个Plunk:http://plnkr.co/edit/PjQdBUq0akXP2fn5sYZs?p=preview
我试图演示使用模态编辑某些数据的常见现实场景.
angular.module('app', ['ngAnimate', 'ui.bootstrap']);
angular.module('app')
.component('myContent', {
template: 'I am content! <button type="button" class="btn btn-default" ng-click="$ctrl.open()">Open Modal</button>',
controller: function($uibModal) {
$ctrl = this;
$ctrl.dataForModal = {
name: 'NameToEdit',
value: 'ValueToEdit'
}
$ctrl.open = function() {
$uibModal.open({
template: '<my-modal greeting="$ctrl.greeting" modal-data="$ctrl.modalData" $close="$close(result)" $dismiss="$dismiss(reason)"></my-modal>',
controller: ['modalData', function(modalData) {
var $ctrl = this;
$ctrl.greeting = 'I am a modal!'
$ctrl.modalData = modalData;
}],
controllerAs: '$ctrl',
resolve: {
modalData: $ctrl.dataForModal
}
}).result.then(function(result) {
console.info("I was closed, so do what I need to do myContent's controller now and result was->");
console.info(result);
}, function(reason) {
console.info("I was dimissed, so do what I need to do myContent's controller now and reason was->" + reason);
});
};
}
});
angular.module('app')
.component('myModal', {
template: `<div class="modal-body"><div>{{$ctrl.greeting}}</div>
<label>Name To Edit</label> <input ng-model="$ctrl.modalData.name"><br>
<label>Value To Edit</label> <input ng-model="$ctrl.modalData.value"><br>
<button class="btn btn-warning" type="button" ng-click="$ctrl.handleClose()">Close Modal</button>
<button class="btn btn-warning" type="button" ng-click="$ctrl.handleDismiss()">Dimiss Modal</button>
</div>`,
bindings: {
$close: '&',
$dismiss: '&',
greeting: '<',
modalData: '<'
},
controller: [function() {
var $ctrl = this;
$ctrl.handleClose = function() {
console.info("in handle close");
$ctrl.$close({
result: $ctrl.modalData
});
};
$ctrl.handleDismiss = function() {
console.info("in handle dismiss");
$ctrl.$dismiss({
reason: 'cancel'
});
};
}],
});
Run Code Online (Sandbox Code Playgroud)
通过传递父控制器不需要使它变得更复杂,您只需从显示模态的.component中访问它.
零件
/**
* @ngdoc component
* @name fsad.component:video
*
* @description <fsad-video> component, in development...
*
*/
(function () {
'use strict';
angular.module('fsad').component('fsadVideo', {
bindings: {},
templateUrl: function(appConstant){return appConstant.paths.modules.fsad + 'leefloon/fsad-video.html'},
controller: controller
});
controller.$inject = ['$scope'];
function controller($scope){
var $ctrl = this;
setDataModel();
/****************************************************************/
$ctrl.ui.close = close;
/****************************************************************/
function setDataModel(){
$ctrl.ui = {};
}
function close(){
$scope.$parent.$close();
}
}
}());
Run Code Online (Sandbox Code Playgroud)
打开模态
var modalInstance = $uibModal.open({
backdrop: 'static',
keyboard: true,
backdropClick: false,
template: '<fsad-video></fsad-video>',
windowClass: 'edit-contactenblad',
});
Run Code Online (Sandbox Code Playgroud)
由于您声明模板是一个组件,因此$ scope.$ parent将始终指向模式实例.允许您访问$ close()函数.
传递和接收数据
如果需要将数据传递给组件,或者从组件接收数据,则可以这样做.
var modalInstance = $uibModal.open({
backdrop: 'static',
keyboard: true,
backdropClick: false,
template: '<fsad-video method="$ctrl.method" on-viewed="$ctrl.userHasViewedVideo(time)"></fsad-ideo>',
controller: function(){
this.method = method;
this.userHasViewedVideo = function(time){}
},
controllerAs: '$ctrl',
windowClass: 'edit-medewerker',
});
Run Code Online (Sandbox Code Playgroud)
只是在旁注,我正在使用此结构样式指南来创建组件.
如果您想要访问$ uibModal $close()
和$dismiss()
函数,以及组件中的一些父数据和函数绑定,您可以一直传递它们:
开放模态逻辑
$uibModal.open({
template: '<login close="$close()" dismiss="$dismiss()" ' +
'email="$ctrl.cookieEmail" check-login="$ctrl.ajaxLogin(user, pass)"></login>',
controller: function () {
this.cookieEmail = $cookies.get('savedEmail');
this.ajaxLogin = AjaxLoginService.login;
},
controllerAs: '$ctrl'
});
Run Code Online (Sandbox Code Playgroud)
模态登录组件
{
templateUrl: 'view/login.html',
bindings: {
email: '<',
checkLogin: '&',
close: '&',
dismiss: '&'
},
controller: function () {
var viewModel = this;
viewModel.password = '';
viewModel.submit = function () {
viewModel.checkLogin(
{ user: viewModel.email, pass: viewModel.password }
).then(function (success) {
viewModel.close();
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
模态HTML
<form ng-submit="$ctrl.submit()">
<input type="text" ng-model="$ctrl.email" />
<input type="password" ng-model="$ctrl.password" />
<button type="button" ng-click="$ctrl.dismiss()">Cancel</button>
<button type="submit">Login</button>
</form>
Run Code Online (Sandbox Code Playgroud)
AngularJS 1.5文档有点稀疏,但它们将&
绑定的用法显示为函数包装器:https://docs.angularjs.org/guide/component
归档时间: |
|
查看次数: |
37062 次 |
最近记录: |