$scope 不从表单输入中获取字符串 - AngularJS、JS [英] $scope doesn't picks up string from form input - AngularJS, JS

查看:14
本文介绍了$scope 不从表单输入中获取字符串 - AngularJS、JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从用户控制面板在 MVC(Rest 服务器)应用程序中实现更改密码功能,但由于一些奇怪的原因,我无法从表单输入中确定值的范围.

我的 html 表单:

,需要一个对象被传递到您的作用域参数中.请注意尝试更改您的父作用域以使用对象并绑定到属性而不是原始值:

$scope.security = {newPassword : '', currentPassword = ''};

然后你会在你的属性中使用这样的东西:

model="security.newPassword"

或者更好的是,不要让它与模型混淆:

myapp-model="security.newPassword"

或者传入整个对象

myapp-security="security"

I am trying to implement Change Password feature in MVC (Rest server) application from the User Control Panel but because of some strange reason I can't scope values from form input.

My html form:

<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>

My 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>

My 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) {

        }
    }
});

My userController:

$scope.changePassword = function() {
    if ($scope.newPassword === $scope.repeatedNewPassword) {
        userService.changePassword($scope.newPassword, $scope.currentPassword);
    } else {
            $scope.alerts.push({
                msg : 'Passwords do not match!'
            })
    }
}

And when I enter values in inputs and place breakpoints and trigger changePassword() in debug i get:

If condition has passed with value of true because they are both undefined.

解决方案

I believe this may be the case of prototypical inheritance and scope, requiring an object being passed into your scoped parameters. Mind trying to change your parent scope to use an object and bind to the properties and not the primitive values:

$scope.security = {newPassword : '', currentPassword = ''};

then you would use something like this in your attributes:

model="security.newPassword"

Or better yet, not make it confusing with model:

myapp-model="security.newPassword"

or pass in the whole object

myapp-security="security"

这篇关于$scope 不从表单输入中获取字符串 - AngularJS、JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆