Dev*_*v01 3 angularjs ionic-framework ionic
我正在使用离子框架,并且需要能够从我的代码中的多个位置调用弹出窗口,所以我想我会将它移动到工厂中.弹出窗口使用输入字段,我想获得它的值.通常我会打电话,$scope.parentGate.answer但因为它在工厂我无法访问范围.我有什么想法可以获得输入字段的值?
这是我的代码:
angular.module('starter.services', [])
.factory('parentGate', function ($ionicPopup, Helpers) {
return {
Show: function(scope, SuccessCallback) {
var first = Helpers.RandomNumber(1, 5) * 10;
var second = Helpers.RandomNumber(11, 22);
var title = 'What does ' + first + ' + ' + second + ' equal?'
// An elaborate, custom popup
return $ionicPopup.show({
template: '<h4 class="text-center">' + title + '</h4><div class="list"><label class="item item-input"><input type="number" ng-model="parentGate.answer" placeholder="Answer..."></label></div>',
title: "Parent Gate",
//subTitle: title,
scope: scope,
buttons: [
{ text: 'Cancel' },
{
text: 'Continue',
type: 'button-positive',
onTap: function(e) {
//
// I can't access $scope.parentGate.answer here.
// How else can I do it?
//
if ($scope.parentGate.answer == first + second) {
console.log("correct");
SuccessCallback();
} else {
console.log("wrong!");
e.preventDefault();
}
}
}
]
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
Tyl*_*ang 11
实际上,您可以访问工厂中的范围.你不能的原因是$scope你的parentGate.show函数中没有调用这样的变量.
似乎您想使用工厂弹出对话框.
我认为在你的情况下,当你尝试调用时,你会将范围作为参数传递给你
angular.module("yourApp").controller("testController", function($scope, parentGate){
parentGate.show($scope, callback);
});
Run Code Online (Sandbox Code Playgroud)
在您的工厂中,当您尝试更改$ scope((onTap回调)下的属性值时,您应该使用scope,而不是 $scope
onTap: function(e) {
//if ($scope.parentGate.answer == first + second) {
if (scope.parentGate.answer == first + second) {
console.log("correct");
SuccessCallback();
} else {
console.log("wrong!");
e.preventDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
这是演示代码.
这就是为什么我们要在你的onTap回调中将$ scope更改为范围的原因(Demo Closure)
希望这会奏效.:)
| 归档时间: |
|
| 查看次数: |
10558 次 |
| 最近记录: |