這是我的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示出和我想和第二輸入框的值來改變它點擊。我怎樣才能做到這一點? 我也在按鈕上添加了一個功能。
大家好,是否有可能更新數組值?例如,我用kapil替換vishal。數組中vishal的值(在js文件中)應該是kapil。 – kapiljain123
它正在更新。看到http://jsfiddle.net/nknrj65z/ –
你可以看到顯示數組,只需將'{{items}}'放在某處並檢查它是如何改變的,即:http://jsfiddle.net/98ey8sjb/ –