javascript - $scope 不从表单输入中获取字符串 - AngularJS,JS

标签 javascript angularjs rest angularjs-scope

我正在尝试从用户控制面板在 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() 时,我得到: enter image description here

如果条件已通过且值为 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/

相关文章:

javascript - XMLHttpRequest - 来自 JavaScript 的 REST

javascript - 将数据存储到 DOM - 元素值与数据属性

c# - 使用谷歌分析记录请求页面时间

javascript - 作用域变量的改变不会影响 View

javascript - AngularJs 在某些条件下显示 ng-repeat 数据

java - Spring 启动: Bypass OncePerRequestFilter filters

javascript - 在电话 01x-xxxxxxxx jquery 上验证

javascript - 如何在node/loopback中同步调用model.find方法?

angularjs - 使用 passport-http Basic + passport-local 组合时如何防止 www-authenticate header

java - 如何在 Spring 请求映射中接受多个可选参数,但一次只能接受一个?