将AngularJS范围变量值与局部变量值分离

时间:2016-05-22 05:48:46

标签: javascript angularjs ionic-framework

$scope.encryptSend = function(payload, tempPwd){
var local = payload;
local.password = Encryption.encrypt(tempPwd);
local.confirm_password = local.password;
$http.post('REST API URL', local).success(function(response) {
  CommonService.setLogin(true);
  $state.go('home.bookings');
});
}


$scope.doRegister = function() {
var tempPwd = $scope.signUpData.password;
var tempCPwd = $scope.signUpData.confirm_password;
var data = $scope.signUpData;

if ($scope.signUpData.tc) {
  if ($scope.signUpData.password === $scope.signUpData.confirm_password) {
    var payload = data;
    $scope.encryptSend(payload, tempPwd);
  }
}
};

以下是HTML代码:

<ion-view title="Signup" id="page6" class=" ">
  <ion-content padding="true" class="has-header">
    <form id="signup-form3" class="list ">
      <ion-list id="signup-list3" class=" ">
        <label class="item item-input item-floating-label" id="signup-input3">
          <span class="input-label">Business Name</span>
          <input type="text" placeholder="Business Name" ng-model="signUpData.business_name">
        </label>
        <label class="item item-input item-floating-label" id="signup-input4">
          <span class="input-label">Username</span>
          <input type="text" placeholder="Username" ng-model="signUpData.username">
        </label>
        <label class="item item-input item-floating-label" id="signup-input5">
          <span class="input-label">Password</span>
          <input type="password" placeholder="Password" ng-model="signUpData.password">
        </label>
        <label class="item item-input item-floating-label" id="signup-input6">
          <span class="input-label">Confirm Password</span>
          <input type="password" placeholder="Confirm Password" ng-model="signUpData.confirm_password">
        </label>

        <label class="item item-input item-floating-label" id="signup-input6">
          <span class="input-label">Email Id</span>
          <input type="email" placeholder="Email Id" ng-model="signUpData.emailId">
        </label>

        <label class="item item-input item-floating-label" id="signup-input6">
          <span class="input-label">Mobile Number</span>
          <input type="number" placeholder="Mobile Number" ng-model="signUpData.mobileNo">
        </label>
        <ion-checkbox ng-model="signUpData.tc">I agree to the Terms & Conditions</ion-checkbox>

      </ion-list>
      <button id="signup-button3" class=" button button-positive  button-block " ng-click="doRegister()">Sign up
      </button>
    </form>
  </ion-content>
</ion-view>

我正在尝试加密密码并将其发送到其余的api。但是,当我单击提交时,表单中的密码文本字段显示加密密码。

我已将整个数据复制到一系列局部变量中。但仍然不知何故$ scope.signupdata反映了加密文本。而且我无法将加密文本与$ scope.signupdata解耦。

请你告诉我这里我做错了什么。

2 个答案:

答案 0 :(得分:0)

我没有对此进行测试,但我认为问题是对象是通过引用传递的,因此var local = payload,将local设置为与$ scope.signUpData相同的对象,而不是它的副本。

另一方面,字符串按值传递。如果您尝试将local设置为空白对象(var local = {})并将字符串复制到其属性,则不应再更新$ scope.signUpData.password,因为引用将被破坏。

答案 1 :(得分:0)

问题是有效负载和local是同一个对象,因此最终更改local.password会更改$scope.signUpData.password。因此,请尝试复制有效负载。

$scope.encryptSend = function(payload, tempPwd){
var local = angular.copy(payload);
local.password = Encryption.encrypt(tempPwd);
local.confirm_password = local.password;
$http.post('REST API URL', local).success(function(response) {
  CommonService.setLogin(true);
  $state.go('home.bookings');
});
}

angular.copy创建了源代码payload的深层副本。它应该可以解决你的问题。