ng-click not working in ng-repeat - angularjs

NG-click not firing in ng -repeat. I want to use ngclick to display only the specific element details.
<body ng-app="mainApp" >
<div ng-app = "mainApp" class="container" ng-controller="TableFilterController">
<table ng-app = "mainApp" class ="table table-hover">
<tr><th>Id</th><th>Name</th><th>Age</th><th>Role</th></tr>
<tr ng-repeat="p in details "><td>{{ $index + 1 }}</td><td><a ng-click="clickMe()">{{p.name}}</a></td><td>{{p.age}}</td><td>{{p.mass}}</td></tr>
</table>
</div>
</body>
js:
var mainApp= angular.module("mainApp", []);
mainApp.controller('TableFilterController', function($scope) {
$scope.clickMe = function(){
alert("hey");
}

There can be different-2 scenarios:
1. Remove Duplicate ng-app.
2.Use This Once,this will test if controller is calling onload or not
mainApp.controller('TableFilterController', function($scope) {
alert("its Calling");
$scope.clickMe = function(){
alert("hey");
}
Check you have mentioned controller name in routing.
4.Check your file's code is loading in browser.
Check these points on first priority otherwise try to create plunker so that everyone can look into your code.

Please try to remove ng-app in table tag, ng-app for application, only once you can use it.
Also no need for a tag for click event because a tag has href attribute.
Use button tag or any other tag rather than a tag.

Try this below code. There were some issues in your code.
The isseus in the code were as below
Script you provided was incomplete. There were some missing }, ] and )
Remove ng-app which are not required. It will not create any issues but its not a good practice to include the same ng-app attribute multiple times.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body ng-app="mainApp">
<div class="container" ng-controller="TableFilterController">
<table class="table table-hover">
<tr><th>Id</th><th>Name</th><th>Age</th><th>Role</th></tr>
<tr ng-repeat="p in details "><td>{{ $index + 1 }}</td><td><a ng-click="clickMe()">{{p.name}}</a></td><td>{{p.age}}</td><td>{{p.mass}}</td></tr>
</table>
</div>
<script src="../lib/angular.js"></script>
<script>
var mainApp= angular.module("mainApp", []);
mainApp.controller('TableFilterController', function ($scope) {
$scope.details = [{ "name": "A1", "age": 10, "mass": 20, },
{ "name": "A2", "age": 15, "mass": 30, },
{ "name": "A3", "age": 20, "mass": 40, },
{ "name": "A4", "age": 30, "mass": 60, }]
$scope.clickMe = function () {
alert("hey");
}
});
</script>

Related

How to do http (ng-repeat) with nested json in angular js?

My code works only when my JSON is not nested. When there is no "," between data and I use only one block of JSON it works.
My Angular:
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<table border="1">
<tr ng-repeat="thing in info" ng-if="thing.color!=null">
<td>{{thing.color}}</td>
<td>{{thing.category}}</td>
<td>{{thing.type}}</td>
</tr>
<tr ng-repeat="thing in info" ng-if="thing.detail!=null">
<td>{{thing.detail}}</td>
<td>{{thing.item}}</td>
<td>{{thing.value}}</td>
</tr>
</table>
<button class="button" ng-click="click()">Button 1</button>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$scope.click = function() {
$http.get("json.js").then(function (response) {
$scope.info=response;
});
};
});
</script>
</div>
</body>
</html>
And my JSON
[
{
"color": "black",
"category": "hue",
"type": "primary"
},
{
"detail": "white",
"item": "red",
"value": "silver"
}
]
Thanks
Please use $scope.info=response.data; instead of $scope.info=response;.

AngularJS: Access elements of inner array

This is a simple html code and it is working fine. Now I have an AngularJS array and I can access and bind variables like $scope.ID or $scope.text which is working fine. But how can I bind values, to $scope.articles array. with relevant values.
For example
If i change the value of f0, (just example it is not working) $scope.articles.[0].betrag_gnetto value should change
If i change the value of f1, (just example it is not working) $scope.articles.[1].betrag_netto value should change
HTML Code (I have created these field through ng-repear loop)
<input type="text" ng-model="betrag_netto0" id="f0">
<input type="text" ng-model="betrag_netto1" id="f1">
<span ng-bind="betrag_netto0"></span>
<span ng-bind="betrag_netto1"></span>
AngularJS Array looks like
{
"isInternalInvoice":1,
"name":"Rechnung",
"ID":"5cd45e86",
"text":null,
"countArticles":2,
"articles":[
{
"ID":"130.123",
"betrag_netto":"123987"
},
{
"ID":"131.123",
"betrag_netto":"1"
}
]
}
Create input fields corresponding to the objects present in the articles array using ng-repeat and this will make it two way binding.
var app = angular.module('plunker', []);
app.controller('ApplicationController', function($scope) {
$scope.obj = {
"isInternalInvoice": 1,
"name": "Rechnung",
"ID": "5cd45e86",
"text": null,
"countArticles": 2,
"articles": [{
"ID": "130.123",
"betrag_netto": "123987"
}, {
"ID": "131.123",
"betrag_netto": "1"
}]
};
});
<!doctype html>
<html ng-app="plunker">
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
</head>
<body>
<div class="container" ng-controller="ApplicationController">
<div class="row" ng-repeat="article in obj.articles">
<input type="text" ng-model="article.betrag_netto" id="f{{$index}}"> <span ng-bind="article.betrag_netto"></span> </div>
</div>
</body>
</html>
try $scope.articles[0].betrag_gnetto
It works

How Can I Bring SQL Data Directly into a Controller in AngularJS

As shown below, in the controller.js, I have tried to use the { name: 'item' } style and it worked in the viewer.
However, I would like to pull the data from the SQL Database.
I tried using {{member.member_name}} but it didn't work.
$http.get('../crud/members_select.php').then(function(response){
$scope.members = response.data;
});
this.items = [
{ member_name: 'Tim' },
{ member_name: 'Dave' },
{ member_name: 'Jenny' }
];
Thus, my question is "How can I do the {{member_name}} thing in order to repeat the data in a single code from SQL Database?"
Below is the HTML CODE.
<div class="ibox-content">
<select ng-options="name as member.member_name for n in members"
multiple
ng-model="selections"
bs-duallistbox></select>
</div>
Thank you in advance!!
You need to do it this way
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example</title>
<script src="https://code.angularjs.org/snapshot/angular.min.js"></script>
</head>
<body ng-app="selectExample">
<script>
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.items = [
{ member_name: 'Tim' },
{ member_name: 'Dave' },
{ member_name: 'Jenny' }
];
$scope.selections = [];
}]);
</script>
<div ng-controller="ExampleController">
<label>Color (null not allowed):
<select ng-model="selections" ng-options="item.member_name for item in items" multiple></select>
</label>
<hr/>
Currently selected: {{ selections }}
</div>
</body>
</html>

Simple controller make angular doesn't work

Hello I am learning angular 1 and I have problem at the very beggining.
I'm trying to display data with table in ng-repeat using controller. Angular in app stops working at all even {{ 2 + 2 }} on bottom of page doesn't work. I can't find the reason why.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SZYBKI START Z BRACKETS</title>
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="bootstrap.min.css">
</head>
<body ng-app="aplikacja">
<div class="container" ng-controller="kontrolerTabeliSkoczkow">
<h1 class="text-center">Klasyfikacja skoczków narciarskich</h1>
<br />
<div class="input-group">
<span class="input-group-addon">Szukaj: </span>
<input type="text" class="form-control" placeholder="np. Adam Małysz" />
</div>
<br />
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th ng-repeat="naglowek in [
'Imię', 'Rok urodzenia', 'Narodowość'
]">{{ naglowek }}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="skoczkowie in skoczkow">
<td>{{skoczek.Name}}</td>
<td>{{skoczek.Nation}}</td>
<td>{{skoczek.rank}}</td>
</tr>
</tbody>
</table>
</div>
</div>
{{ 2+2 }}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script>
var app = angular.module('aplikacja', []);
app.contoller( 'kontrolerTabeliSkoczkow' , ['$scope', function( $scope ) {
$scope.skoczkowie = [
{
"Name": "HIRSCHER Marcel",
"Nation": "AUT",
"rank": 1
},
{
"Name": "JANSRUD Kjetil",
"Nation": "NOR",
"rank": 2
}
]
}];
</script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>
You forgot to close the controller.
<script>
var app = angular.module('aplikacja', []);
app.contoller( 'kontrolerTabeliSkoczkow' , ['$scope', function( $scope ) {
$scope.skoczkowie = [
{
"Name": "HIRSCHER Marcel",
"Nation": "AUT",
"rank": 1
},
{
"Name": "JANSRUD Kjetil",
"Nation": "NOR",
"rank": 2
}
]
}]);
</script>
(The ')' at the very end)
Also, I would declare Jquery BEFORE angular
It's typo mistake, change
app.contoller(...)
to
app.controller(...)
and close properly:
app.controller("kontrolerTabeliSkoczkow" , ['$scope', function( $scope ) { ... }]);
see link jsfiddle
A parenthesis is missing just between the ]} and ; at the end of your controller
Furthermore, I do not recommand using this notation, just don't declare dependencies in your controller, I recommand doing it this way :
app.controller("kontrolerTabeliSkoczkow", function($scope){
/*anything you want to put in it*/
});
You don't get confused by parenthesis, brackets and stuff, just declare your dependencies in your module, not in your controller

Angular indexOf not outputting text

I'm trying to output the name "Sam" on the screen in a ng-show using indexOf, but nothing ever appears. Any help is appreciated.
html
<head></head>
<body>
<div ng-controller="ArrayController">
<div ng-repeat="product in products">
<div ng-show="product.name.indexOf('Sam') == 2">
</div>
</div>
</div>
<script src="bower_components/angular/angular.js"></script>
<script src="app.js"></script>
</body>
</html>
angular
var app = angular.module('myApp', []);
app.controller('ArrayController', ['$scope', function($scope){
$scope.products = [
{
name: 'Joe'
},
{
name: 'Bill'
},
{
name: 'Sam'
}
];
}]);
Possible solution using ng-if instead of ng-show
<body ng-app="myApp">
<div ng-controller="ArrayController">
<div ng-repeat="product in products">
<div ng-if="product.name.indexOf('Sam') > -1">{{product.name}}
</div>
</div>
</div>
</body>
var app = angular.module('myApp', []);
app.controller('ArrayController', ['$scope', function($scope){
$scope.products = [
{
name: 'Joe'
},
{
name: 'Bill'
},
{
name: 'Sam'
}
];
}]);
<div ng-show="product.name.indexOf('Sam') == 2"></div>
This will show the div if product.name contains the string "Sam" starting at its third character (i.e. "xxSam"). That probably isn't what you want.
The div is empty, so will not be visible to the user even if the ng-show is truthy. That, too, is probably not what you want.
I'm pretty sure what you do want is this:
<div>{{product.name}}</div>

Resources