我正在尝试从用户控制面板在 MVC(Rest 服务器)应用程序中实现更改密码功能,但由于某些奇怪的原因,我无法从表单输入中确定值的范围。
我的 html 表单:
<accordion-group heading="Change password" is-open="changePasswordStatus.open" style="cursor: pointer">
<div>
<div>
<form class="form-horizontal" role="form">
<form-row model="newPassword" name="New: " size="col-sm-8"></form-row>
<form-row model="repeatedNewPassword" name="Repeat: " size="col-sm-8"></form-row>
<form-row model="currentPassword" name="Current: " size="col-sm-8"></form-row>
<br>
</form>
</div>
<div>
<button class="btn btn-default btn-sm" ng-click="changePassword()">Save</button>
<button class="btn btn-warning btn-sm" ng-click="changePasswordStatus.open = !changePasswordStatus.open">Cancel</button>
</div>
</div>
</accordion-group>
我的 formRow.html:
<div class="form-group">
<label for="inputText3" class="col-sm-2 control-label">{{name}}</label>
<div class="{{size}}">
<input type="{{type}}" class="form-control" data-ng-model="model">
</div>
</div>
我的 formRow.js:
collectionsApp.directive('formRow', function(){
return {
restrict: 'E',
replace: true,
scope: {
model: '=',
name: '@',
size: '@',
type: '@'
},
templateUrl: '/directives/formRow.html',
link: function(scope, attrs, element) {
}
}
});
我的用户 Controller :
$scope.changePassword = function() {
if ($scope.newPassword === $scope.repeatedNewPassword) {
userService.changePassword($scope.newPassword, $scope.currentPassword);
} else {
$scope.alerts.push({
msg : 'Passwords do not match!'
})
}
}
当我在输入中输入值并放置断点并在调试中触发 changePassword() 时,我得到:
如果条件已通过且值为 true,因为它们都未定义。
最佳答案
我相信这可能是 prototypical inheritance and scope 的情况,需要将一个对象传递到您的作用域参数中。介意尝试更改您的父范围以使用对象并绑定(bind)到属性而不是原始值:
$scope.security = {newPassword : '', currentPassword = ''};
然后你会在你的属性中使用这样的东西:
model="security.newPassword"
或者更好的是,不要让它与模型混淆:
myapp-model="security.newPassword"
或者传入整个对象
myapp-security="security"
关于javascript - $scope 不从表单输入中获取字符串 - AngularJS,JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25530564/