角JS工厂里面访问范围 [英] Access scope inside an angular js factory
问题描述
我使用的是离子型框架,需要能够调用从muliple地方弹出我的code,所以我想我会把它转移到一个工厂。弹出窗口使用一个输入框,我想它的价值。一般情况下我只是叫 $ scope.parentGate.answer
,但因为它是在工厂里,我没有访问的范围。任何想法如何,我可以得到输入字段的值?
下面是我的code:
angular.module('starter.services',[])
.factory('parentGate',函数($ ionicPopup,佣工){
返回{
显示:功能(范围,SuccessCallback){
变种第一= Helpers.RandomNumber(1,5)* 10;
VAR秒= Helpers.RandomNumber(11,22);
VAR标题='什么是'+第一+++第二+'相等?
//一个精心策划的,自定义弹出
返回$ ionicPopup.show({
模板:'< H4类=TEXT-中心>' +标题+'< / H4>< DIV CLASS =清单><标签类=逐项输入><输入类型=数字NG-模式=parentGate.answer占位符=回答......>< /标签>< / DIV>,
标题:父门,
//字幕:标题,
适用范围:适用范围,
纽扣: [
{文字:'取消'},
{
文字:继续,
输入:'按钮阳性',
中的onTap:功能(E){ //
//我不能在这里访问$ scope.parentGate.answer。
//我还能怎么办呢?
//
如果($ scope.parentGate.answer ==第一+第二){ 的console.log(正确);
SuccessCallback();
}其他{
的console.log(错!);
亦即preventDefault();
}
}
}
]
});
}
};
});
在事实上,你可以访问你的工厂的范围。究其原因,你不能是有没有这样的一个名为 $范围
在parentGate.show函数的变量。
似乎要使用一个工厂,弹出一个对话框。
我觉得你的情况,你会通过你的范围作为参数时ü尝试调用
angular.module(yourApp)。控制器(的TestController功能($范围,parentGate){
parentGate.show($范围,回调);
});
而在你的工厂,当您尝试更改在$范围,(中的onTap回调),你应该使用属性值范围
,不可以 $范围
中的onTap:功能(E){
//如果($ scope.parentGate.answer ==第一+第二){
如果(scope.parentGate.answer ==第一+第二){
的console.log(正确);
SuccessCallback();
}其他{
的console.log(错!);
亦即preventDefault();
}
}
这里是演示code。结果
这就是为什么我们要改变$范围范围,您的的onTap原因回调(演示关闭)
希望这会工作。 :)
I'm using the ionic framework and need to be able to call a popup from muliple places in my code so I thought I would move it into a factory. The popup uses an input field and I want to get the value of it. Normally I would just call $scope.parentGate.answer
but because it's in a factory I don't have access to the scope. Any ideas how I can get the value of the input field?
Here's my code:
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();
}
}
}
]
});
}
};
});
In fact, you can access to the scope in your factory. The reason you cannot is there's no such a variable called $scope
in your parentGate.show function.
Seems you want to use a factory to pop up a dialog.
I think in your case, you will pass you scope as a parameter when u try to invoke
angular.module("yourApp").controller("testController", function($scope, parentGate){
parentGate.show($scope, callback);
});
And in your factory, when you try to change the property value under $scope, (onTap callback) you should use scope
, not $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();
}
}
Here is the demo code.
Here is the reason why we want to change $scope to scope in your onTap callback (Demo Closure)
Hope this will work. : )
这篇关于角JS工厂里面访问范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!