2015-04-01 27 views
0

我跟随this guide执行身份验证到我的Angular应用程序,我无法访问我的控制器内的模型。

在我的应用程序

地方,我打开模态窗口是这样的:

loginModal().then(function() { 
    // Successful login, proceed to the page the user wants 
    return $state.go(toState.name, toParams); 
}).catch(function() { 
    // Login failed, return home 
    return $state.go('home'); 
}); 

loginModal()是一种服务,它看起来像这样:

myApp.service('loginModal', function($modal, $localStorage) { 
    function assignCurrentUser(user) { 
     $localStorage.user = user; 
     return user; 
    } 

    return function() { 
     var instance = $modal.open({ 
      templateUrl: '/partials/modals/login.html', 
      controller: 'LoginController', 
      windowClass: 'small' 
     }); 

     return instance.result.then(assignCurrentUser); 
    }; 
}); 

当使用loginModal()服务,它打开包含/partials/modals/login.html文件的HTML页面上的模式窗口。这一切都完美。您会注意到,我还指定要在模式中使用的控制器:LoginController

LoginController写为这样:

myApp.controller('LoginController', function($scope) { 
    $scope.login = function() { 
     console.log($scope.user); 
    }; 
}); 

,最后,这是在模态窗口中使用的部分:

<form name="loginForm" novalidate method="post" ng-submit="login();"> 
    <label>Email address 
     <input type="email" name="email" ng-model="user.email"> 
    </label> 
    <label>Password 
     <input type="password" name="password" ng-model="user.password"> 
    </label> 
    <input type="submit" value="Login"> 
</form> 

的问题是,当我填写表格(通过填充user.emailuser.password)并单击提交按钮,我将undefined打印到控制台。

基本上,从我LoginController$scope.login功能确实得到执行,但$scope.user属性未定义即使它应该通过形式在窗口模式来填充(使用的ng-model S)。

此外,如果我将{{user.email}}放入模态模板中,然后填充电子邮件字段,它将打印。所以我知道双向数据绑定的作品。

为什么我无法从控制器内的模态窗口访问$scope值?

+0

解决了这个问题,或者您仍然需要任何东西? – 2015-04-05 07:26:27

回答

0

现在不要现在,如果它有帮助,但我有这样的路由问题。 解决方案是使用ng-model="$parent.user.name"$parent添加到控制器中的依赖关系列表中,并使用$scope.$parent.user.email 或仅仅$scope.user.email调用它,有时它有效。 希望对你有所帮助。

+0

不,没有任何工作:(尽管我想你可能会推动我在正确的方向弄清楚这一点! – 2015-04-01 17:45:50