I think I'm missing something but cannot figure what.
Basically I'm trying to pass an object to the modal like below, but instead of getting the passed object I gets null...so I think is a problem with the scope but I'm new in Angular and need some help.
我想我错过了什么,但我不知道是什么。基本上,我尝试将一个对象传递给模态,如下所示,但是我没有获得传递的对象,而是获得null…所以我认为这是范围的问题,但我是角度的新手,需要一些帮助。
Controller
控制器
app.controller("musicViewModel", function ($scope, $http, $location, $uibModal, $log) {
$scope.selected = null;
$scope.open = function (item) {
$scope.selected = item;
$log.info('Open' + $scope.selected); // get right passes object
var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
controller: 'musicViewModel',
size: 'lg',
resolve: {
items: function () {
return $scope.selected;
}
}
});
};
$scope.toggleAnimation = function () {
$scope.animationsEnabled = !$scope.animationsEnabled;
};
});
View
视图
<div class="row" ng-controller="musicViewModel">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
<ul>
<li>
{{ selected }} // always gets null
</li>
</ul>
</div>
</script>
</div>
4 个解决方案
#1
63
I'd suggest you to pass the scope
of your own controller instead of passing same controller
again, by doing that you can remove the resolve
also.
我建议您传递您自己的控制器的范围,而不是再次传递相同的控制器,通过这样做,您也可以删除解决方案。
var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
scope: $scope, //passed current scope to the modal
size: 'lg'
});
Otherwise you need to create a new controller
and assign that controller for modal
while opening it.
否则,您需要创建一个新的控制器,并在打开它时为modal分配该控制器。
#2
23
When you use resolve, the map is injected into the given controller.
当您使用resolve时,映射被注入到给定的控制器中。
I recommend that u use a different controller to handle the modal functionality (separation of concerns).
我建议您使用不同的控制器来处理模式功能(关注点分离)。
I also recommend to use dependency injection to support minification of the code. Step 5 on the Angular tutorial wil explain this.
我还建议使用依赖注入来支持代码的缩小。第5步,在角度教程中,wil解释了这一点。
A simplified example of the modal controller would be.
模态控制器的一个简化示例是。
(function () {
'use strict';
var app = angular.module('App');
app.controller('musicDetailController',
['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.items = items;
}]);
}());
#3
2
You cannot pass an object directly.
不能直接传递对象。
I've tried all the solutions above, but wasn't really satisfied. I've solved the issue by writing a simple parser that enables you to pass both strings
and objects
directly to the modal, through the provided resolve function.
我尝试了以上所有的解决方案,但并不满意。我通过编写一个简单的解析器来解决这个问题,该解析器允许您通过提供的resolve函数将字符串和对象直接传递给modal。
app.controller('ModalController', ['$uibModal', '$scope', function ($uibModal, $scope) {
// Initialize $modal
var $modal = this;
// Open component modal
$modal.open = function (component, size, data) {
// Init modal
var modalInstance = $uibModal.open({
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
component: component,
size: size || 'md',
resolve: parseResolve(data)
});
};
// Parse the resolve object
function parseResolve(data) {
if (typeof data === 'string') {
return {
data: function() {
return data;
}
}
}
else if (typeof data === 'object') {
var resolve = {};
angular.forEach(data, function(value, key) {
resolve[key] = function() {
return value;
}
})
return resolve;
}
}
}]);
When usings strings
当usings字符串
Template:
模板:
<button ng-click="$modal.open('modalSomething', 'md', 'value'">
Click
</button>
Component:
组件:
bindings: {
resolve: '@'
}
When using objects
当使用对象
Template:
模板:
<button ng-click="$modal.open('modalSomething', 'md', {key1: value1, key2: value2})">
Click
</button>
Component:
组件:
bindings: {
resolve: '<'
}
#4
-1
I got the below code working:
我让下面的代码工作:
this.OpenModal = function() {
var modalInstance = $uibModal.open({
url: "/name?parameter=" + $scope.Object.ParamValue,
templateUrl: 'views/module/page.html',
controller: myController
});
}
#1
63
I'd suggest you to pass the scope
of your own controller instead of passing same controller
again, by doing that you can remove the resolve
also.
我建议您传递您自己的控制器的范围,而不是再次传递相同的控制器,通过这样做,您也可以删除解决方案。
var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
scope: $scope, //passed current scope to the modal
size: 'lg'
});
Otherwise you need to create a new controller
and assign that controller for modal
while opening it.
否则,您需要创建一个新的控制器,并在打开它时为modal分配该控制器。
#2
23
When you use resolve, the map is injected into the given controller.
当您使用resolve时,映射被注入到给定的控制器中。
I recommend that u use a different controller to handle the modal functionality (separation of concerns).
我建议您使用不同的控制器来处理模式功能(关注点分离)。
I also recommend to use dependency injection to support minification of the code. Step 5 on the Angular tutorial wil explain this.
我还建议使用依赖注入来支持代码的缩小。第5步,在角度教程中,wil解释了这一点。
A simplified example of the modal controller would be.
模态控制器的一个简化示例是。
(function () {
'use strict';
var app = angular.module('App');
app.controller('musicDetailController',
['$scope', '$uibModalInstance', 'items',
function ($scope, $uibModalInstance, items) {
$scope.items = items;
}]);
}());
#3
2
You cannot pass an object directly.
不能直接传递对象。
I've tried all the solutions above, but wasn't really satisfied. I've solved the issue by writing a simple parser that enables you to pass both strings
and objects
directly to the modal, through the provided resolve function.
我尝试了以上所有的解决方案,但并不满意。我通过编写一个简单的解析器来解决这个问题,该解析器允许您通过提供的resolve函数将字符串和对象直接传递给modal。
app.controller('ModalController', ['$uibModal', '$scope', function ($uibModal, $scope) {
// Initialize $modal
var $modal = this;
// Open component modal
$modal.open = function (component, size, data) {
// Init modal
var modalInstance = $uibModal.open({
ariaLabelledBy: 'modal-title',
ariaDescribedBy: 'modal-body',
component: component,
size: size || 'md',
resolve: parseResolve(data)
});
};
// Parse the resolve object
function parseResolve(data) {
if (typeof data === 'string') {
return {
data: function() {
return data;
}
}
}
else if (typeof data === 'object') {
var resolve = {};
angular.forEach(data, function(value, key) {
resolve[key] = function() {
return value;
}
})
return resolve;
}
}
}]);
When usings strings
当usings字符串
Template:
模板:
<button ng-click="$modal.open('modalSomething', 'md', 'value'">
Click
</button>
Component:
组件:
bindings: {
resolve: '@'
}
When using objects
当使用对象
Template:
模板:
<button ng-click="$modal.open('modalSomething', 'md', {key1: value1, key2: value2})">
Click
</button>
Component:
组件:
bindings: {
resolve: '<'
}
#4
-1
I got the below code working:
我让下面的代码工作:
this.OpenModal = function() {
var modalInstance = $uibModal.open({
url: "/name?parameter=" + $scope.Object.ParamValue,
templateUrl: 'views/module/page.html',
controller: myController
});
}