我試圖將顯示頁面上顯示的列表信息傳遞給該頁面上的模式。角度工廠對象綁定到未出現在Modal中的作用域
我成功創建了工廠服務,它返回了一個對象。
angular.module('articles').factory('ProductService', [ '$resource', 'Articles','$stateParams', function($resource, Articles, $stateParams) {
var listingInfo =
Articles.get({
articleId: $stateParams.articleId
});
return listingInfo;
}
]);
(通過使用angular.element(document.body).injector().get('ProductService')
記錄)如果我把這個在我的主ArticlesController
我能看到通過瀏覽器控制檯範圍與angular.element($0).scope()
並能夠通過注射到我的控制器訪問對象和給它的範圍$scope.product = ProductService;
,允許我以預期的方式訪問數據(product.furtherinfo
)。
但是,當我嘗試相同的技術爲我的模態控制器時,我無法找到範圍,當我通過瀏覽器登錄或通過綁定或括號訪問數據。
我已經嘗試通過該解決方案傳遞值,在我的所有控制器中注入依賴項必須使用我的模態,但沒有任何工作。
// Modals
angular.module('articles').controller('ModalDemoCtrl',['$scope', '$modal', '$log', 'ProductService' , function ($scope, $modal, $log, ProductService) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.product = ProductService;
$scope.animationsEnabled = true;
$scope.open = function (size) {
var modalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function() {
return $scope.items;
},
product: function() {
return $scope.product;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.toggleAnimation = function() {
$scope.animationsEnabled = !$scope.animationsEnabled;
};
}]);
的想法是返回工廠對象傳遞給我的模式,所以我可以將其鏈接到我可以指定爲模型通過對發送到電子郵件的輸入(也許隱藏)。
angular.module('articles').controller('ModalInstanceCtrl',['$scope', '$modalInstance', 'items', '$http', 'product','ProductService','$stateParams', function ($scope, $modalInstance, items, $http, product,ProductService,$stateParams) {
$scope.items = items;
$scope.product = product;
$scope.sendMail = function(){
var data = ({
input : this.contactAgentInput,
inputBody : this.contactAgentInputBody,
})
$http.post('/contact-agent', data).
then(function(response) {
// this callback will be called asynchronously
$modalInstance.close($scope.selected.item);
console.log("all is well")
// when the response is available
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
})
}
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
}]);
你在做什麼? ProductService正在直接注入您的模態。請讓你的問題更簡潔。 –
我想在我的模式下訪問我的ProductService對象。所以我可以自動填充我的模態輸入的值與列表信息。例子''會給我顯示頁面上看到的地址。希望我清楚,如果不是,我會詳細說明。 –
您正在將'ProductService'注入到'ModalInstanceCtrl'中。你爲什麼不使用它? –