简体   繁体   English

为什么我的范围函数没有从模型传递数据?

[英]Why is my scope function not getting data passed from the model?

I have a simple angular app that asks the user for a first and last name, but for some reason my changeBothNames() function is not getting the firstName and lastName models passed to it with the new values. 我有一个简单的角度应用程序,要求用户提供名字和姓氏,但是由于某种原因,我的changeBothNames()函数未获得使用新值传递给它的firstName和lastName模型。

Here is my app as it stands: 这是我目前的应用程序:

 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> 

The model is obviously updating since you can see the output on the screen but for some reason its not being passed correctly, any ideas? 该模型显然正在更新,因为您可以在屏幕上看到输出,但是由于某种原因,它没有正确传递,有什么想法吗?

The values are being passed in but alert only takes one parameter. 传递值,但alert仅采用一个参数。 Change it to console.log and you'll see. 将其更改为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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM