gpt4 book ai didi

javascript - Ionic - 没有在工厂内的 $ionicPopup.show 中返回值 $scope

转载 作者:行者123 更新时间:2023-11-29 10:37:04 25 4
gpt4 key购买 nike

我用 $ionicPopup 创建了一个项目。我将 $ionicPopup 代码放在 .factory 中。在我的 $ionicPopup.show() 中,我要求用户输入一个值。用户已经输入值后,它会提示用户输入的值。

我也看了下面的帖子,还是不能解决我的问题Access scope inside an angular js factory .

这是我的代码:

控制者

.controller('PopupCtrl',function($scope, $ionicPopup, $timeout, popupService) {

// Triggered on a button click, or some other target
$scope.showPopup = function() {

var showParameter = {
title: "Test",
cssClass: "",
subTitle: "Insert any value",
template: '<input type="text" ng-model="value">',
templateUrl: "",
buttons: {
cancelText: "Reject",
cancelType: "button-assertive",
okText: "Accept",
okType: "button-positive"
}
}

// An elaborate, custom popup
popupService.show(showParameter, $scope).then(function(res) {
console.log('Tapped!', res);
alert("value: " + res);
});
};
})

工厂

.factory('popupService', function ($ionicPopup) {
return{
show: function(param, scope){
var show = $ionicPopup.show({
title: param.title, // String. The title of the popup.
cssClass: param.cssClass, // String, The custom CSS class name
subTitle: param.subTitle, // String (optional). The sub-title of the popup.
template: param.template, // String (optional). The html template to place in the popup body.
templateUrl: param.templateUrl, // String (optional). The URL of an html template to place in the popup body.
scope: scope, // Scope (optional). A scope to link to the popup content.
buttons: [{ // Array[Object] (optional). Buttons to place in the popup footer.
text: param.buttons.cancelText,
type: param.buttons.cancelType,
onTap: function(e) {
return false;
}
}, {
text: param.buttons.okText,
type: param.buttons.okType,
onTap: function(e) {
// Returning a value will cause the promise to resolve with the given value.
return scope.value;
}
}]
});
return show;
}
}
});

演示: http://codepen.io/aishahismail/pen/pgpdGW?editors=101

确实需要您的帮助。谢谢。

最佳答案

由于 JS(和 Angular)对象继承[1],你必须在对象中“包装”基元,所以这里是工作代码(从你的代码中 fork 出来):

http://codepen.io/beaver71/pen/JGMvdV

主要修改如下:

$scope.data = {};

...

template: '<input type="text" ng-model="data.value">'

[1] Popup 从 Controller 继承其作用域。一般见:https://github.com/angular/angular.js/wiki/Understanding-Scopes

关于javascript - Ionic - 没有在工厂内的 $ionicPopup.show 中返回值 $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34919933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com