0

Hi ,

I m write a code I m showing the two section and one form for add new text in my projects.

But i have two major Problem .

  1. is if i click to edit button than show the form but if i change any text in form than i click to save button there is a nothing happen .

  2. is if i fill the data in new form and just press submit button not add the data in my top li Please check this and solve my problem .

Thanks in advanced Please help me

My Code is here

Angular Code is this

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

app.controller('mySearchController', function($scope) {
  $scope.searhBars = [{
    title: "fiel 1",
    description: 'What do you want'
  }, {
    title: "fiel 2",
    description: "I want to this"
  }, {
    title: "fiel 3",
    description: "Why do you want me "
  }];

  $scope.formSubmit = function(searhBar) {
    showForm = false;
  };


  $scope.newItemAdd = function(title, description) {
    if (this.newName === '') return;
    $scope.searhBar.push({
      title: title,
      description: description
    });
    this.newName = '';
    this.newDes = '';
  };
});

HTML Code is

<body ng-app="myApp">
  <ul ng-controller="mySearchController">
    <li>Hello</li>
    <li ng-repeat="searhBar in searhBars">


      <h4 ng-show="!showForm">{{searhBar.title}}</h4>
      <p ng-show="!showForm">{{searhBar.description}}</p>
      <button ng-show="!showForm" ng-click="showForm=true">Edit me</button>



      <form ng-submit="formSubmit(searhBar)" ng-show="showForm">
        <label>Field 1.
          <input type="text" ng-model="searhBar.title" />
        </label>
        <label>Enter Description
          <textarea ng-model="searhBar.description"></textarea>
        </label>
        <input type="submit" value="Save" />
      </form>


    </li>

    <li>
      <form ng-submit="newItemAdd(newName, newDes)">
        <label>Field
          <input type="text" ng-model="newName" />
        </label>
        <label>Enter Description
          <textarea ng-model="newDes"></textarea>
        </label>
        <input type="submit" value="submit" />
      </form>
    </li>
  </ul>
</body>

Demo is

3 Answers 3

2

Is this what you wanted?

http://plnkr.co/edit/9ginpPiLUaVyEPdpu32f?p=preview

I fixed this part:

$scope.formSubmit = function(){
 this.showForm = false;
};


$scope.newItemAdd = function(title, description){
  if(this.newName === '') return ;
  $scope.searhBars.push({
    title:title,
    description:description
  });
  this.newName= '';
  this.newDes= '';
};

And this one:

    <input type="submit" ng-click="formSubmit()" />

Oh, I also fixed this:

    <label>Field {{$index + 1}}:

In order to display the correct number of the field.

Sign up to request clarification or add additional context in comments.

Comments

2

Typo: $scope.searhBar[s].push({

for close form

<form ng-submit="formSubmit(searhBar); showForm=false"

http://plnkr.co/edit/Vq3U3sCcp4OeZlH1iXyM?p=preview

Comments

1

You need to write:

$scope.formSubmit = function(searhBar){
    this.showForm = false;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.