我跟随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.email
和user.password
)并单击提交按钮,我将undefined
打印到控制台。
基本上,从我LoginController
的$scope.login
功能确实得到执行,但$scope.user
属性未定义即使它应该通过形式在窗口模式来填充(使用的ng-model
S)。
此外,如果我将{{user.email}}
放入模态模板中,然后填充电子邮件字段,它将打印。所以我知道双向数据绑定的作品。
为什么我无法从控制器内的模态窗口访问$scope
值?
解决了这个问题,或者您仍然需要任何东西? – 2015-04-05 07:26:27