2013-05-22 80 views
1

范围的问题,我有一个页面,包括NG-开关:AngularJS范围对象这里

<div ng-switch on="pagename()"> 
     <div ng-switch-when="plans"> 
      <div ng-include="'/assets/angular/plans/existingplans.html'"></div> 

     </div> 
     <div ng-switch-when="new"> 
      <div ng-include="'/assets/angular/plans/newplans.html'"></div> 
     </div> 
     <div ng-switch-when="credits"> 
      <div ng-include="'/assets/angular/plans/creditspanel.html'"></div> 
     </div> 
     <div ng-switch-when="wizard"> 
      <div ng-include="'/assets/angular/plans/wizard.html'"></div> 
     </div> 

    </div> 

正如你可以看到我打开控制器的方法,这种方法势必会一个范围变量,它附带一个RESTful数据源:

$scope.pagename = function() { 
      var loc = $location.path().split("/").pop(); 

      if (loc == "plans" && $scope.plansinfo.data.length <1) { 
       return "wizard"; 
      } else if (loc == "plans" && $scope.plansinfo.data.length >=1){ 
       return "plans"; 
      } else if(loc == "new"){ 
       return "new"; 
      } 
      else if(loc == "wizard"){ 
       return "wizard"; 
      } 
      else { 
       // alert(loc) 
       console.log(loc); 
       console.log(typeof $scope.plansinfo.data); 

       return "credits"; 
      } 
     }; 

其基本思想是将用户驱动到正确的页面。问题出在你登陆的那个页面上,并且附加了$ scope.plansinfo数据源对象,但是当你遍历页面的时候,其他页面没有定义这个对象。

所以简而言之,我在控制器中做了什么错误,以便在嵌套的作用域中创建对象,而不是顶级作用域?

感谢

汤姆

==== ====编辑

在试图清理东西我也跟着下面的建议,将代码移动到服务,但现在我得到的是:

TypeError: string is not a function 

这里是我的尝试:

.factory('PlansData', function() { 
    var data = {}; 
    data.plansinfo = {}; 

    return { 
     updateinfo: function(newdata) { 
      data.plansinfo = newdata; 
     }, 
     pagename: function (location) { 
       return 'credits'; 
     }, 
     plansinfo: data 
    } 
}) 

控制器:

 $scope.pagename = PlansData.pagename($location); 

所以它应该只是返回字符串,而是我得到上述错误。我错过了什么?

感谢

汤姆

+0

尝试设置'在'$ rootScope' $ scope.plansinfo'。这应该让你不管你是什么样的孩子目前的范围英寸 –

+0

在哪个位置要设置$ scope.plansinfo –

+0

嗨阿贾伊,乔纳森访问它,该rootscope思想的伟大工程,但我不知道如何“最佳实践”它是。我在PlansCtrl控制器中设置了plansinfo变量,他们都是共享的,我之前尝试过使用嵌套的Controller,但似乎没有获得任何地方。 – magicaltrout

回答

0

这是使用服务来管理你的模型数据,而不是一个控制器的好时机。您可能已经使用过一项服务来管理您的RESTful API,因此您可能在那里包含pagename函数。如果不是,这里是创建一个新的服务来管理数据的一个例子:

app.service('locService', function(){ 
    this.pagename = function() { 
     // switch function here 
    }; 
}); 

然后你可以注入该服务将需要的信息控制器:

app.controller('myController', function (locService){ 
    $scope.pagename = locService.pagename; 
}); 
+0

嗨sh0ber,您的评论是正确的,但是当我移动到功能我厂所有我得到告诉是类型错误:字符串不是编辑后,以显示我已经尝试 – magicaltrout

+0

嗨,我想这可能是一个功能 在你的应用程序中的其他错误的结果。 [这是一个小提琴](http://jsfiddle.net/sh0ber/BABsK/),显示控制器使用该服务。 – sh0ber

+0

实际上,它看起来像问题是你在传递'$ location'服务的地方需要一个字符串。 – sh0ber

0

What am I doing wrong in my controller so that the object is being created in the nested scope, not the top level scope?

如果您在你PlansCtrl使用原始的,或者你重新分配对象,它不会工作:

var someObj = { hello: 'there' }; 

function MyCtrl($scope) { 
    $scope.plansinfo = {}; // object, okay 
    console.log($scope); 
} 
function MySubCtrl($scope) { 
    $scope.plansinfo = someObj; // oops, reassigned the object 
    console.log($scope); 
} 

以上会发生什么为t帽子控制器MySubCtrl将获得一个名为plansinfo的本地属性,该属性隐藏/遮蔽同名的MyCtrl属性。这是the way JavaScript works

分配到子控制器的对象属性应该工作:

var someObj = { hello: 'there' }; 

function MyCtrl($scope) { 
    $scope.plansinfo = {}; 
    console.log($scope); 
} 
function MySubCtrl($scope) { 
    $scope.plansinfo.obj = someObj; 
    console.log($scope); 
} 

fiddle

什么上面的情况是,控制器MySubCtrl的外观和找不到它$scope定义对象属性plansinfo.obj,然后它使用原型继承并在其父级上找到该对象,因此它在那里被设置。

+0

嗨,马克,感谢试图解释这样对我,我已经通过关于继承和对象VS原始的东西几个文件,我一直在试图理解它,我认为在创造$范围。 plansinfo然后分配我的数据到$ scope.plansinfo.data这是创建对象? – magicaltrout

+0

@magicaltrout,如果你有'$ scope.plansinfo = {};'你PlansCtrl和'$ scope.plansinfo.data = someObj中;'的范围是prototypically继承PlansCtrl(最终),它应该工作。如果没有,也许你可以尝试设置一个小提琴或撬棍。 –