2016-06-08 32 views
4

这是我的Html和JavaScript代码,我想通过函数改变它。如何用函数更改ng-init值?

的Html

<div class="container" ng-app="myApp" ng-controller="myController"> 
    <h2>{{title}}</h2> 
    <ul ng-init="initItems()"> 
    <li ng-repeat="item in items"> 
     <input ng-model="item.name" type="text"/> 
     <div ng-if="makeVisible == item.id && makeVisible !='' "> 
     <input ng-model="newname" type="text"/> 
     <button ng-click="hideme(item.id); rename()" type="button"> 
      <span class="glyphicon glyphicon-ok"> 
      </span> 
     </button> 
     <button ng-click="hideme(item.id)" type="button"> 
      <span class="glyphicon glyphicon-remove"> 
      </span> 
     </button> 
     </div> 
     <input ng-click=" showme(item.id)" type="button" value="Rename"/> 
    </li> 
    </ul> 
</div> 

的JavaScript

function item(id, name) { 
    this.id = id; 
    this.name = name; 
}; 

angular.module('myApp', []).controller('myController', function($scope) { 
    $scope.items = []; 
    $scope.makeVisible = ""; 
    $scope.initItems = function() { 
     $scope.items.push(new item("0", 'Vikash')); 
     $scope.items.push(new item("1", 'Kumar')); 
     $scope.items.push(new item("2", 'Vishal')); 
     $scope.items.push(new item("3", 'Ajay')); 
    }; 
    $scope.renameThis = function(index, oldValue) { 
     console.log("oldValue==" + oldValue); 
     console.log("indexx==" + index); 
     var id = 'box-' + index; 
     console.log("Id : " + id); 
     document.getElementById(id).style.display = "block"; 
     console.log('jai hind'); 
    }; 
    $scope.showme = function(id) { 
     $scope.makeVisible = id; 
     console.log('id', id); 
    }; 
    $scope.hideme = function(id) { 
     console.log(item); 
     $scope.makeVisible = ""; 
    }; 
    $scope.title = 'Enter new name here'; 
    $scope.rename = function() { 
     $scope.item.name = $scope.newname; 
     console.log('dfasdd'); 
    }; 
}); 

以下是在ng-init值这是在输入框1示出和我想和第二输入框的值来改变它点击。我怎样才能做到这一点? 我也在按钮上添加了一个功能。

回答

1

新增可见ATTR项,

function item(id, name) { 
    this.id = id; 
    this.visible=false; 
    this.name = name; 
} 

和变化显示和隐藏功能,这些代码

$scope.hideme = function(item) { 
    item.visible=false; 
}; 

$scope.showme = function(item) { 
    item.visible=true; 
}; 

和更改此代码

<div ng-if="makeVisible == item.id && makeVisible !='' "> 

到:

<div ng-if="item.visible"> 

,当你正在使用ng-if的NEWNAME车型将只提供当产品在命名方式发送项目对象shomme和hideme功能

<input ng-click="showme(item)" type="button" value="Rename"/> 


<button ng-click="hideme(item); rename()" type="button"> 
    <span class="glyphicon glyphicon-ok"> 
    </span> 
</button> 
<button ng-click="hideme(item)" type="button"> 
    <span class="glyphicon glyphicon-remove"> 
    </span> 
</button> 
1

所以,你可以这样做:

function item(id, name) { 
 
    this.id = id; 
 
    this.name = name; 
 
}; 
 

 
angular 
 
    .module('myApp', []) 
 
    .controller('myController', function($scope) { 
 
     $scope.items = []; 
 
     $scope.makeVisible = ''; 
 
     $scope.title = 'Enter new name here'; 
 
     $scope.initItems = function() { 
 
      $scope.items.push(new item('0', 'Vikash')); 
 
      $scope.items.push(new item('1', 'Kumar')); 
 
      $scope.items.push(new item('2', 'Vishal')); 
 
      $scope.items.push(new item('3', 'Ajay')); 
 
     }; 
 
     $scope.showme = function(id) { 
 
      $scope.makeVisible = id; 
 
     }; 
 
     $scope.hideme = function(id) { 
 
      $scope.makeVisible = ''; 
 
     }; 
 
     $scope.rename = function(item, newname) { 
 
      item.name = newname; 
 
     }; 
 
    });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div class="container" ng-app="myApp" ng-controller="myController"> 
 
    <h2>{{title}}</h2> 
 
    <ul ng-init="initItems()"> 
 
     <li ng-repeat="item in items"> 
 
      <input ng-model="item.name" ng-disabled="true" type="text"/> 
 
      <div ng-if="makeVisible === item.id && makeVisible !== ''"> 
 
       <input ng-model="newname" type="text" /> 
 
       <button ng-click="hideme(item.id); rename(item, newname);" type="button"> 
 
        <span class="glyphicon glyphicon-ok"></span> 
 
       </button> 
 
       <button ng-click="hideme(item.id)" type="button"> 
 
        <span class="glyphicon glyphicon-remove"></span> 
 
       </button> 
 
      </div> 
 
      <input ng-if="makeVisible !== item.id" ng-click="showme(item.id)" type="button" value="Rename"/> 
 
     </li> 
 
    </ul> 
 
</div>

1

据我了解,你想与第二个输入框的值来改变第一个输入框(item.name)的值(newname)按功能rename触发点击(可以说是重命名按钮)。

解决方案是通过itemnewname来运行。

简体HTML:

<ul ng-init="initItems()"> 
    <li ng-repeat="item in items"> 
     <input ng-model="item.name" type="text" /> 
     <input ng-model="newname" type="text" /> 
     <input ng-click="rename(item, newname)" type="button" value="Rename" /> 
    </li> 
</ul> 

控制器:

$scope.rename = function(item, newname_) { 
    item.name = newname_; 
}; 

见工作http://jsfiddle.net/vp2r52pb/

+0

大家好,是否有可能更新数组值?例如,我用kapil替换vishal。数组中vishal的值(在js文件中)应该是kapil。 – kapiljain123

+0

它正在更新。看到http://jsfiddle.net/nknrj65z/ –

+0

你可以看到显示数组,只需将'{{items}}'放在某处并检查它是如何改变的,即:http://jsfiddle.net/98ey8sjb/ –