2016-09-23 65 views
0

我在我的AngularJS(版本1.4)应用程序中有一个工厂。 的代码是在主app.js文件:角厂不保留数据

.factory('UserPreferences', function($http, URL, AuthService, store){ 

    var userPreferences = { 
     "face_shape": '', 
     "lifestyle": '', 
     "hair_texture": '', 
     "email": '', 
     "postcode": '', 
     "full_name": '', 
     "password": '' 
    }; 

    return { 
     save_face_shape: function(face_shape){ 
      userPreferences.face_shape = face_shape; 
      console.log(userPreferences); // -> LINE 307 
     }, 
     save_lifestyle: function(lifestyle){ 
      userPreferences.lifestyle = lifestyle; 
      console.log(userPreferences); // -> LINE 311 
     }, 
     save_hair_texture: function(texture){ 
      userPreferences.hair_texture = texture; 
     }, 
     get_data: function(){ 
      return userPreferences; 
     } 
    } 
}). // -> here's another factory 

我试图做的是通过应用不同的步骤来保存用户prefernces,并且触发事件后保存在数据库中的整个用户。

在页面的控制,我这样做,我有以下代码:

$scope.triggerSecondStep = function(shape){ 
    UserPreferences.save_face_shape(shape); 
    $scope.step = 2; 
} 

$scope.triggerThirdStep = function(lifestyle){ 
    UserPreferences.save_lifestyle(lifestyle); 
    $scope.step = 3; 
} 

在控制器的观点,我称这些功能在一个非常简单的方法:

<img ng-click="triggerSecondStep('square')" class='hover-opacity' ng-src="LINK_IMAGE" /> 

由于没有工作,我添加了一些console.log打印出中间步骤。我发现的是,上线307打印出console.log以下输出,当我打电话triggerSecondStep

app.js:307 Object {face_shape: "round", lifestyle: "", hair_texture: "", email: "", postcode: ""…} 
app.js:307 Object {face_shape: undefined, lifestyle: "", hair_texture: "", email: "", postcode: ""…} 

console.log在线路311的输出如下:

app.js:311 Object {face_shape: undefined, lifestyle: "busy", hair_texture: "", email: "", postcode: ""…} 
app.js:311 Object {face_shape: undefined, lifestyle: undefined, hair_texture: "", email: "", postcode: ""…} 

所以基本上我不知道为什么数据不会从一次调用存储到另一次调用,以及为什么console.log只输出一次输出的两倍而不是一次。

任何人都可以帮忙吗?

编辑:

阅读评论后更新问题(谢谢btw)。

如果我加入一些console.logtriggerSecondStep

$scope.triggerSecondStep = function(shape){ 
    console.log(shape); 
    UserPreferences.save_face_shape(shape); 
    console.log(UserPreferences.get_data()); 
    $scope.step = 2; 
} 

这是输出(landing_compiled简直landing.js的编译版本,与巴贝尔):

square 
app.js:308 Object {face_shape: "square", lifestyle: "", hair_texture: "", email: "", postcode: ""…} 
landing.compiled.js:72 Object {face_shape: "square", lifestyle: "", hair_texture: "", email: "", postcode: ""…} 
landing.compiled.js:70 undefined 
app.js:308 Object {face_shape: undefined, lifestyle: "", hair_texture: "", email: "", postcode: ""…} 
landing.compiled.js:72 Object {face_shape: undefined, lifestyle: "", hair_texture: "", email: "", postcode: ""…} 

如果我添加debugger; at line 307我看到下面的stackstrace:

enter image description here

一旦我到达最后一步(r.handle),它只是重新开始,这次没有参数。

+1

您是否有任何其他对'triggerS的引用econdStep'呢?它看起来好像某个地方你有其他东西没有参数调用它,并覆盖你刚刚保存的值。 – Duncan

+0

在行307添加一个'debugger;'语句,并在运行脚本时打开检查器。您可以检查堆栈跟踪以查看谁调用了此方法。其中一个应该是'$ scope.triggerSecondStep' –

+0

你可以记录数据进入你的控制器功能吗?例如$ scope.triggerSecondStep = function(shape){console.log(shape) UserPreferences.save_face_shape(shape); $ scope.step = 2; } – user2085143

回答

-1

我创建拨弄着相同的数据,请您参考:

Fiddle

var myApp = angular.module('myApp',[]); 
+0

谢谢你的小提琴,我可以看到这是工作......在我的应用程序中可能是什么问题? – ste

+0

你可以用小提琴更新你的数据,以便我可以检查吗?或者你更新你的问题? –

1

你们是对的:基本触发triggerSecondStep有一个div该函数的图像元素外在ng-click上调用相同的功能:

<div ng-click="triggerSecondStep()" class='title-widget margin-bottom'>What's your face <span class='transparent-font'>shape</span>?</div> 
... 
... 
... 
<img ng-click="triggerSecondStep('square')" class='hover-opacity' ng-src="LINK_IMAGE" />