如何在提交时设置角度变化值?

时间:2015-07-08 17:00:00

标签: javascript html angularjs angularjs-scope

有人可以告诉我该怎么做,这样当我按下"提交"按钮myName变量是否更新?谢谢!

app.js:

app.controller('SomeController', ['$scope', 'emails', function($scope, emails) {
  emails.success(function(data) {

    $scope.emails = data;
  });

    $scope.myName = "John"; 

}]); 

view.html

<div ng-controller="SomeController">
    <form name="messageForm" ng-submit="update(myName)">
      <input type="text" ng-model="myName"/>
        <h1>{{myName}}</h1>
      <input type="submit" value="Send Message"/>
    </form>
    </div>

2 个答案:

答案 0 :(得分:3)

只要您键入:

,这将更新
<input type="text" ng-model="myName"/>
<h1>{{myName}}</h1>

只有在您点击提交时才会更新:

<form name="messageForm" ng-submit="update(myName)">
  <input type="text" ng-model="myName"/>
    <h1>{{myUpdatedName}}</h1>
  <input type="submit" value="Send Message"/>
</form>
您应该在控制器上

$scope.update = function(name) {
  $scope.myUpdatedName = name;
};

请记住,您的表单应该包含在宣布您的SomeController的任何组件中...例如

<div ng-controller="SomeController">
  <!-- your form goes here -->
</div>

答案 1 :(得分:0)

看起来您错过了控制器中的更新功能:

app.controller('SomeController', ['$scope', 'emails', function($scope, emails) {
  emails.success(function(data) {

    $scope.emails = data;
  });

  $scope.myName = "John"; 
  $scope.update = function(name) {
    $scope.myName = name;
  };

}]);