2016-05-13 14 views
0

我有我的html页面的表单:形式angularjs如何显示当前状态,同时增加NG-模型

<div id=update> 
     <form class="form-inline" ng-submit="updateCompany(company.companyId,company.companyName,company.newPassword,company.newEmail)" ng-show="updateForm"> 
       <h3>Update Company</h3> 
      <div class="form-group"> 

       <input type="text" 
        class="form-control" id="companyId" value={{company.companyId}} readonly/> 
      </div> 

      <div class="form-group"> 

       <input type="text" 
        class="form-control" id="companyName" 
        value={{company.companyName}} readonly/> 

      </div> 
      <div class="form-group"> 

       <input type="text" 
        class="form-control" id="companyPassword" 
        placeholder="Enter New Password" ng-model="company.newPassword"/> 

      </div> 
      <div class="form-group"> 

       <input type="email" 
        class="form-control" id="companyEmail" placeholder="Enter New Email" 
        ng-model="company.newEmail" /> 
        <button type="submit" class="btn btn-default">UPDATE</button> 
      </div> 
     </form> 
</div> 

我想显示当前公司价值(ID,姓名,密码,电子邮件) ,而不是让用户选择更改密码和电子邮件,并在提交表单时发送所有参数。

问题是,当我将ng-model放在文本字段上时,当前值消失。 我需要一个修复!

在前两个字段中,我现在看到了这个值,因为我没有ng-model这个值,一旦我把ng-model消失了。

+0

要么使用NG-init或定义一些NG-模型这些领域,并将其设置为任何你想要的显示。 – yBrodsky

+0

[AngularJS - 输入文本框中的值属性在使用ng模型时会被忽略?](http://stackoverflow.com/questions/10610282/angularjs-value-attribute-on-an-put -text-box-is-ignored-when-is-a-ng-m) – Saad

+0

我成功地将我的数据传给了我的控制器中的$ scope.something,并且我可以在ng-model的文本字段中看到它,现在当用户更改那些我想用submit =“”作为参数发送它们的参数时,任何人都可以帮我解决这个问题吗? –

回答

0

在你的控制器就在公司数据附加到的范围是这样的:

$scope.company = yourcompanydata 

至于提交的数据,你不必列出您的HTML所有参数。在你的HTML见好就收:

ng-submit="updateCompany()" 

而在你的控制器:

$scope.updateCompany = function(){ 
    // your submitting logic here and all the company data will 
    // be available under $scope.company 
    // including the new password and email entered by the user 

    // so your submitting logic could look something like this: 
    submitCompanyData($scope.company.companyId, $scope.company.newPassword,...) 
} 
+0

感谢每一个机构!它现在好了! –

0

下面是一个简单的版本codepen让你开始,这取决于你想后记数据做什么。我可以根据需要进行更新。

angular 
 
    .module('app', []) 
 
    .controller('ExampleController', ExampleController); 
 

 
function ExampleController() { 
 
    var vm = this; 
 
    vm.company = {}; 
 
    vm.info = info; 
 

 
    function info(info) { 
 
    console.log(info); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<body ng-app='app'> 
 
    <div class="container" ng-controller="ExampleController as vm"> 
 
    <form novalidate> 
 
     <input type="text" ng-model="vm.company.name" required/> 
 
     <input type="email" ng-model="vm.company.email" required/> 
 
     <input type="submit" ng-click="vm.info(vm.company)" value="Submit" /> 
 
    </form> 
 
    {{vm.company| json}} 
 
    </div> 
 
</body>

相关问题