2017-04-23 43 views
0

我陷入了奇怪的问题。其实我是从ajax获取数据并在文本框中显示它。Angularjs中的嵌套ng控制器问题

所以,基本上我有3个文本框(City,City1,City2)。默认城市文本框是可见的,如果他们有来自Ajax的数据,休息会显示。

用户可以通过单击+按钮添加城市或通过单击 - 按钮删除。

我能够在文本框上正确提取并显示数据。

但是当我想通过点击+按钮添加/显示City1或城市。它只是执行表单提交事件。

这是用于Ajax调用的代码。

formApp.controller('getprofile', function($scope,$http){ 
    $http({ 
          url: 'get_job_city.php', 
          method: "GET", 
          params: {uid: uid} 
         }) 
        .success(function(data) { 

         if (data.success) { 

         $scope.formData.city1 = data.city1; 
        $scope.formData.city = data.city; 
         $scope.formData.city2 = data.city2;   
          } 
        }); 
        }) 

表单保存和显示隐藏城市文本框的代码。

var formApp = angular.module('formApp', []); 

    formApp.controller('formProfile1', function($scope,$http){ 

     $scope.secondcity1city1 = false; 
     $scope.thirdcity2 = false;  

    $scope.hidecity1 = function() { $scope.secondcity1 = false; } 
    $scope.hidecity2 = function() { $scope.thirdcity2 = false; }  

$scope.showcity = function() { $scope.secondcity1 = true; } 
$scope.showcity1 = function() { $scope.thirdcity2 = true; }  


     $scope.formData = {}; 

     $scope.formprofile1 = function() { 
      $scope.ajaxload = true; 
      var allData={'formData': $scope.formData, 'uid': uid} 
     $http({ 
       method : 'POST', 
       url  : 'city_update_exec.php', 
       data : allData, 
       headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers 
      }) 
       .success(function(data) { 
      $scope.ajaxload = false; 
        if (!data.success) { 
         $scope.errorcity = data.errors.city; 
         $scope.errorcity1 = data.errors.city1; 
         $scope.errorcity2 = data.errors.city2;   


        }else{ 

        alert('City has been updated.'); 
        } 

       }); 


     }; 

    }) 

HTML代码如下。

<div class="container" ng-controller="formProfile1"> 
    <form name="formProfile1" method="post" id="formProfile1" 
     ng-submit="formprofile1()" role="form"> 
    <div ng-controller ="getprofile">  
     <div id="firstcity"> 
     <div class="col-xs-10"> 
      <div class="topjob_resumetitle" ng-class="{ 'has-error' : errorcity }"> 
      <input name="city" id="city" type="text" 
        class="form-control textbox1 txt-auto" 
        required="required" placeholder="Job Location* " 
        ng-model="formData.city"> 
      <div class = "errorba" ng-show="errorcity">{{errorcity}} 
      </div> 
      </div> 
     </div> 
     <div class="col-xs-2"> 
      <!--<button class="remove" ng-click="removeChoice()">-</button>--> 
     </div> 


     <button class="addfields" ng-click="showcity()">+</button><br> 
     </div> 

     <div ng-show="secondcity1"> 
     <div class="col-xs-9"><div class="topjob_resumetitle" ng-class="{ 'has-error' : errorcity1 }"> 
      <input name="city1" id="city1" type="text" 
       class="form-control textbox1 txt-auto" 
       placeholder="Job Location* " ng-model="formData.city1"> 
      <div class = "errorba" ng-show="errorcity">{{errorcity1}} 
      </div> 
     </div> 
     </div> 
     <div class="col-xs-3"> 
     <button class="remove" ng-click="hidecity1()">-</button> 
     <button class="addfields" ng-click="showcity1()">+</button><br> 
     </div> 

    </div> 

    <div ng-show="thirdcity2"> 
     <div class="col-xs-10"><div class="topjob_resumetitle" 
      ng-class="{ 'has-error' : errorcity2 }"> 
     <input name="city2" id="city2" type="text" 
      class="form-control textbox1 txt-auto" 
      placeholder="Job Location* " 
      ng-model="formData.city2"> 
     <div class = "errorba" ng-show="errorcity2">{{errorcity2}} 
     </div> 
    </div> 
    </div> 
    <div class="col-xs-2"> 
    <button class="remove" ng-click="hidecity2()">-</button> 
    </div> 

     More text boxes are here 

</div> 
+1

的更多的代码有穿过去,就越有可能人们可以找到你的问题。简化您的示例。请参见[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 – georgeawg

+0

我会创建一个小提琴。 – Ironic

回答

1

的问题在于你没有为你的按钮设置一个类型,因此默认提交类型,当点击时它将提交在DOM中找到的第一个父表单。为了让您的自定义点击处理程序正确执行,改变你的按钮类型“按钮”,例如:

<button type="button" class="addfields" ng-click="showcity()">+</button><br>

+0

你救了我。你是我能说的天才。你发现它。我很害怕,由于大代码没有人会帮助我。但你做到了。 – Ironic

0

要发出http请求,您不需要使用单独的控制器。在您的代码中使用工厂方法,并将其注入到控制器中作为依赖项。请参阅Angular Factory method

1

Remove方法=“邮报”的形式标记添加的novalidate将停止HTML5验证

<form name="formProfile1" novalidate id="formProfile1" 
    ng-submit="formprofile1()" role="form"> 

此外,如果它仍然没有工作,然后更改所有的按钮

<input type="button">//for rest of the buttons 
<input type="submit">//for submit button 
+0

你答案的第二部分是正确的。键入=“按钮”它是需要的。投了票。 – Ironic