我有一个包含几个控制器和ng-includes的页面。 在遇到实际问题之前,我应该详细介绍一下我的恶意应用程序
它位于节点之上并表达 它正在与快递上运行的护照进行通信以进行认证 现在我希望为经过身份验证的用户和未经身份验证的用户提供相同的页面,但有一些差异,让我们说第一个更改是导航栏中的配置文件选项 -
ul.nav.navbar-nav.navbar-right(ng-show="identity.isAuthenticated()")
此无序列表具有向经过身份验证的用户显示的选项,一旦我登录它就会正常工作,这意味着变量身份和方法 isAuthenticate 工作正常 但问题是配置文件形式是一个引导模式框 -
div.modal.fade(id="profileModal")
div.modal-dialog
div.modal-content
div.modal-header
button.close(data-dismiss="modal") ×
h4 Update Profile
div.modal-body
form.form-horizontal(name="profileForm")
fieldset
.form-group
label.col-md-2.control-label(for="email") Email
.col-md-10
input.form-control(name="email", type="email", placeholder="Email", ng-model="email", required)
.form-group
label.col-md-2.control-label(for="fname") First Name
.col-md-10
input.form-control(name="fname", type="text", placeholder="First Name", ng-model="fname", required)
.form-group
label.col-md-2.control-label(for="lname") Last Name
.col-md-10
input.form-control(name="lname", type="text", placeholder="Last Name", ng-model="lname", required)
.form-group
label.col-md-2.control-label(for="password") Password
.col-md-10
input.form-control(name="password", type="password", placeholder="Password", ng-model="password")
.form-group
div.modal-footer
button.btn.btn-primary(ng-click="update()", ng-disabled="profileForm.$invalid") Submit
|
a.btn.btn-default(data-dismiss="modal") Cancel
现在我在这里使用不同的控制器 mvProfileCtrl ,如下所示 -
angular.module('app').controller('mvProfileCtrl', function($scope, mvAuth, mvIdentity, mvNotifier) {
$scope.email = mvIdentity.currentUser.username;
$scope.fname = mvIdentity.currentUser.firstName;
$scope.lname = mvIdentity.currentUser.lastName;
$scope.update = function() {
var newUserData = {
username: $scope.email,
firstName: $scope.fname,
lastName: $scope.lname
}
if($scope.password && $scope.password.length > 0) {
newUserData.password = $scope.password;
}
mvAuth.updateCurrentUser(newUserData).then(function() {
mvNotifier.notify('Your user account has been updated');
}, function(reason) {
mvNotifier.error(reason);
})
}
})
这里的问题是,只要我打开页面并且用户未登录,则currentUser是空对象,因此它内部没有任何内容。 所以我在控制台中得到了这个错误 -
TypeError: Cannot read property 'username' of undefined
at new <anonymous> (http://localhost:5000/app/account/mvProfileCtrl.js:2:42)
at invoke (http://localhost:5000/vendor/angular/angular.js:3899:17)
at Object.instantiate (http://localhost:5000/vendor/angular/angular.js:3910:23)
at http://localhost:5000/vendor/angular/angular.js:7164:28
at http://localhost:5000/vendor/angular/angular.js:6566:34
at forEach (http://localhost:5000/vendor/angular/angular.js:327:20)
at nodeLinkFn (http://localhost:5000/vendor/angular/angular.js:6553:11)
at compositeLinkFn (http://localhost:5000/vendor/angular/angular.js:6007:15)
at compositeLinkFn (http://localhost:5000/vendor/angular/angular.js:6014:13)
at publicLinkFn (http://localhost:5000/vendor/angular/angular.js:5916:30)
但是,只要我刷新页面,currentUser就会拥有所有必需的数据,这就是为什么没有错误。那么,我如何确保在用户通过身份验证后,只将$ scope变量绑定到此控制器,或者之后才启动控制器。
项目来源有点大,这就是为什么我没有在这里包含完整的细节,所以如果我错过任何细节请建议
编辑::删除mvIdentity和currentUser部分之后的另一个可能的错误,当我尝试这个时调试 -
angular.module('app').controller('mvProfileCtrl', function($scope, mvAuth, mvNotifier) {
$scope.update = function() {
console.log($scope.email);
var newUserData = {
username: $scope.email,
firstName: $scope.fname,
lastName: $scope.lname
}
if($scope.password && $scope.password.length > 0) {
newUserData.password = $scope.password;
}
mvAuth.updateCurrentUser(newUserData).then(function() {
$('#profileModal').modal('toggle');
mvNotifier.notify('Your user account has been updated');
}, function(reason) {
mvNotifier.error(reason);
})
}
});
它给了我undefined,这表明我的$ scope没有绑定到profileForm对象,但如果我使用观察者然后使用之前建议的代码,那么我发现输入元素确实有currentUsers的firstname和lastname告诉$ scope附加在那里。我不明白这里发生了什么,请任何人解释一下
我正在应用这样的控制器 -
div(ng-include="'/partials/account/authNavBar'",ng-controller="mvProfileCtrl")
其中authNavBar具有此引导模式代码和profileForm
答案 0 :(得分:1)
使用范围和观察者对您有利。如果您通过对服务的引用在范围上设置mvIdentity,则可以添加观察者,并仅在currentUser存在时更新这些属性。
angular.module('app').controller('mvProfileCtrl', function($scope, mvAuth, mvIdentity, mvNotifier) {
$scope.mvIdentity = mvIdentity;
$scope.$watch('mvIdentity.currentUser', function(currentUser) {
if (!currentUser) return;
$scope.email = currentUser.username;
$scope.fname = currentUser.firstName;
$scope.lname = currentUser.lastName;
});
$scope.update = function() {
var newUserData = {
username: $scope.email,
firstName: $scope.fname,
lastName: $scope.lname
}
if($scope.password && $scope.password.length > 0) {
newUserData.password = $scope.password;
}
mvAuth.updateCurrentUser(newUserData).then(function() {
mvNotifier.notify('Your user account has been updated');
}, function(reason) {
mvNotifier.error(reason);
})
}
});