Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
73
rated 0 times [  75] [ 2]  / answers: 1 / hits: 22896  / 10 Years ago, sat, may 10, 2014, 12:00:00

I have put all necessary files into a single file. I want to push item into array when the user clicks on a button. Here is the content:



When I click on the button, nothing happens. Also the data is not repeated/looped and {{}} is shown in angular which means there is a problem.



<script type=text/javascript src=angular.js ></script>
<div data-ng-app=App>
<div data-ng-controller=MyController>
<ul data-ng-repeat=one in names>
<li>{{ one.first }}</li>
</ul>
</div>
</div>

<input type=text data-ng-model=Namer.name/>
<input type=submit data-ng-click=AddName()/>


<script type=text/javascript>
var App = angular.module(App, []);

App.controller(MyController, function($scope){
$scope.names = [
{ first : Thomas},
{ first : Geferson},
{ first : Jenny},
{ first : Maria},
];

$scope.AddName = function(){
$scope.names.push({
name : $scope.Namer.name;
});
};
});
</script>

More From » angularjs

 Answers
5

Working DEMO



var App = angular.module(App, []);

App.controller(MyController, function ($scope) {
$scope.names = [{
first: Thomas
}, {
first: Geferson
}, {
first: Jenny
}, {
first: Maria
}];

$scope.AddName = function () {
$scope.names.push({
first: $scope.Namer.name
});
};
});


You need to move your data-ng-click inside Controller.Also you had some syntax issues.That is also i fixed (To work with IE ALSO)



<div data-ng-app=App>
<div data-ng-controller=MyController>
<ul data-ng-repeat=one in names>
<li>{{ one.first }}</li>
</ul>
<input type=text data-ng-model=Namer.name />
<input type=submit data-ng-click=AddName() />
</div>
</div>

[#71099] Thursday, May 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sashacitlallik

Total Points: 30
Total Questions: 100
Total Answers: 85

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
;