javascript - 为什么我的范围函数没有从模型中获取数据?

标签 javascript angularjs

我有一个简单的 Angular 应用程序,它要求用户输入名字和姓氏,但出于某种原因,我的 changeBothNames() 函数没有获得传递给它的 firstName 和 lastName 模型的新值。

这是我目前的应用:

var app = angular.module("myApp", []);

app.controller("mainCtrl", ["$scope",
  function($scope) {
    $scope.message = "hello";
    $scope.firstName = "";
    $scope.lastName = "";
    $scope.fullName = $scope.firstName + " " + $scope.lastName;
    $scope.updateName = function() {
      $scope.fullName = $scope.firstName + " " + $scope.lastName;
    }
    $scope.changeBothNames = function(first, second) {
      alert("aye", first, second);
      $scope.firstName = first;
      $scope.lastName = second;
      $scope.updateName();
    }
  }
]);
[ng-app="myApp"] [ng-controller="mainCtrl"] {
  max-width: 750px;
  margin: 50px auto 0 auto;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-model="firstName"],
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-model="lastName"] {
  padding: 5px;
  border: 0;
  border-bottom: 1px solid #ccc;
  margin-right: 10px;
  outline: 0;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-click="changeBothNames(firstName, lastName)"] {
  border: 0;
  color: ghostwhite;
  padding: 15px;
  font-weight: 700;
  background-image: -webkit-linear-gradient(top, #00B6FF, #002D40);
  background-image: linear-gradient(to bottom, #00B6FF, #002D40);
  -webkit-transition: all .25s ease-in-out;
  transition: all .25s ease-in-out;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-click="changeBothNames(firstName, lastName)"]:hover {
  -webkit-transform: translateY(-2px);
  transform: translateY(-2px);
  box-shadow: 0 5px 2px black;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [id="output"] [id="greeting"] {
  font-weight: 700;
  text-transform: capitalize;
  font-size: 24px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="mainCtrl">
    <div class="row">
      <input ng-model="firstName" placeholder="Enter a first name">
      <input ng-model="lastName" placeholder="Enter a last name">
      <button ng-click="changeBothNames(firstName, lastName)">Update Name</button>
    </div>
    <div class="row">
      <div id="output" ng-if="fullName = ' ' ">
        <div id="greeting">{{firstName}} {{lastName}}</div>
      </div>
      <div id="output" ng-if="fullName != ' ' ">
        <div id="greeting">{{message}}</div>{{fullName}}
      </div>
    </div>
  </div>

模型显然正在更新,因为您可以在屏幕上看到输出,但由于某种原因它没有被正确传递,有什么想法吗?

最佳答案

正在传递值但是 alert只接受一个参数。将其更改为 console.log,您就会看到。

var app = angular.module("myApp", []);

app.controller("mainCtrl", ["$scope",
  function($scope) {
    $scope.message = "hello";
    $scope.firstName = "";
    $scope.lastName = "";
    $scope.fullName = $scope.firstName + " " + $scope.lastName;
    $scope.updateName = function() {
      $scope.fullName = $scope.firstName + " " + $scope.lastName;
    }
    $scope.changeBothNames = function(first, second) {
      console.log("aye", first, second);
      document.getElementById('message').innerHTML = first + ' ' + second;
      $scope.firstName = first;
      $scope.lastName = second;
      $scope.updateName();
    }
  }
]);
[ng-app="myApp"] [ng-controller="mainCtrl"] {
  max-width: 750px;
  margin: 50px auto 0 auto;
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
  -ms-flex-direction: column;
  flex-direction: column;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-model="firstName"],
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-model="lastName"] {
  padding: 5px;
  border: 0;
  border-bottom: 1px solid #ccc;
  margin-right: 10px;
  outline: 0;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-click="changeBothNames(firstName, lastName)"] {
  border: 0;
  color: ghostwhite;
  padding: 15px;
  font-weight: 700;
  background-image: -webkit-linear-gradient(top, #00B6FF, #002D40);
  background-image: linear-gradient(to bottom, #00B6FF, #002D40);
  -webkit-transition: all .25s ease-in-out;
  transition: all .25s ease-in-out;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [ng-click="changeBothNames(firstName, lastName)"]:hover {
  -webkit-transform: translateY(-2px);
  transform: translateY(-2px);
  box-shadow: 0 5px 2px black;
}
[ng-app="myApp"] [ng-controller="mainCtrl"] [id="output"] [id="greeting"] {
  font-weight: 700;
  text-transform: capitalize;
  font-size: 24px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="mainCtrl">
    <div class="row">
      <input ng-model="firstName" placeholder="Enter a first name">
      <input ng-model="lastName" placeholder="Enter a last name">
      <button ng-click="changeBothNames(firstName, lastName)">Update Name</button>
    </div>
    <div class="row">
      <div id="output" ng-if="fullName == ' ' ">
        <div id="greeting">{{firstName}} {{lastName}}</div>
      </div>
      <div id="output" ng-if="fullName != ' ' ">
        <div id="greeting">{{message}}</div>{{fullName}}
      </div>
    </div>
  </div>
  <pre id="message"></pre>

关于javascript - 为什么我的范围函数没有从模型中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36318573/

相关文章:

javascript - 无法通过背景单击关闭自定义 UI Bootstrap 模式

javascript - AngularJS RequireJS Browserify 和 Javascript 模块/全局范围的噩梦

angularjs - Ng-重复 : Filter By Uniqueness

javascript - Backbone 嵌套 View

javascript - 如何从 element.style 中删除多个 css 样式

javascript - 对具有相似日期时间的对象数组进行重复数据删除

angularjs - 有没有AngularJS方式可以在我的 Controller 中按下某个键来调用例程?

javascript - 我的前端 javascript 文件无法访问其他 javascript 文件中的变量

javascript - 简单的纯 Javascript 拖动 Controller slider

javascript - 如何在 MVC cshtml 页面的 @string.Concat() 中使用 Angular 范围变量