I'm trying to adapt one of the many to-do list examples built with AngularJS and LocalStorage to a little app of mine.
So far I'm able to create and save items into the LocalStorage, and to delete items both from the view and the LocalStorage. The problem I'm finding now is that when I delete items, the delete function won't delete the item I chose, but the first from the list.
For instance, if I have an array of 1-2-3-4 and I choose to delete the 3, it will always delete the 1, and then the 2, and so on.
Here's my code, being a beginner as I am, I'm sure I missed something...
angular.module('Logger', [])
.controller('TodoCtrl',['$scope', function($scope){
$scope.todos = extractJSONFromLocalStorage();
$scope.addTodo = function () {
$scope.todos.push({ text: $scope.formTodoText});
jsonToLocalStorage($scope.todos);
$scope.formTodoText = '';
};
$scope.kill = function (index) {
$scope.todos.splice(index, 1);
localStorage.removeItem(index);
jsonToLocalStorage($scope.todos);
};
function extractJSONFromLocalStorage() {
return JSON.parse(localStorage.getItem("todo")) || [
{text: 'learn angular', done: false},
{text: 'eat food', done: false},
{text: 'Click to remove', done: true}
];
}
function jsonToLocalStorage(todos) {
var jsonTodo = angular.toJson(todos);
if (jsonTodo != 'null') {
localStorage.setItem("todo", jsonTodo);
} else {
alert("Invalid JSON!");
}
}
}]);
And the HTML:
<h1><span class="modal-title">Edit drivers</span></h1>
<li ng-repeat="todo in todos">
<span class="done-true">{{todo.text}}</span>
<label class="delete pull-right" for='{{todo.text}}' ng-click="kill(index)">
<div><i class="fa fa-close"></i></div>
</label>
</li>
<h2>Add new driver<h2>
<form action="" class="form-horizontal controller" ng-submit="addTodo()">
<input class="form-control" type="text" placeholder="Enter driver name" ng-model="formTodoText" ng-model-instant>
<button class="btn btn-success form-control">Add</button>
</form>
Any tips would be highly appreciated!
try putting $index instead of index:
<label class="delete pull-right" for='{{todo.text}}' ng-click="kill($index)">
Related
I have a select option and file upload control. The default value of dropdown is set to select. I have a file upload control, where on load the upload button should be disabled/hidden and when user selects an item other than select in dropdown the upload button should be enabled, and a custom message should be shown about the option selected in dropdown. I have tried the below, but seems to be an issue. In index.html
<body ng-controller="Main as vm">
<div class="container">
<br />
<div class="row">
<div class="col-sm-2">
<label class="control-label">Select Environment :<em style="color:red">*</em></label>
</div>
<div class="col-sm-4 form-group">
<select name="mySelect" id="mySelect" class="dropdown form-control cl-sm-6" ng-options="option.name for option in data.availableOptions track by option.id" ng-model="data.selectedOption" ng-change="selectEnvironmentChanged()" ></select>
</div>
<label ng-show="vm.showWarning">Warning message</label>
<label ng-show="vm.showEnvironmentMessage">You have selected </label>
</div>
<div class="row">
<div class="form-group" >
<label for="newfiles">Select and upload files</label>
<input type="file" id="imageUploadfile" class="uploadFile" accept="image/*" my-files="files" ngf-multiple="false" />
</div>
<div class="col-sm-4 form-group">
<input class="btn btn-primary" type="button" ng-if="showBtns" ng-show="showUpload" ng-click="uploadNewFiles()" value="upload" />
</div>
</div>
</div>
In the controller page, I have code as
(function () {
'use strict';
var myApp = angular.module('app',[]);
myApp.controller('Main', function ($scope) {
$scope.showUpload = false;
$scope.data = {
availableOptions: [
{ id: '1', name: 'Select' },
{ id: '2', name: 'aY1' },
{ id: '3', name: 'aY3' },
{ id: '4', name: 'bA4' }
],
selectedOption: { id: '1', name: 'Select' }
};
$scope.selectEnvironmentChanged = function () {
if ($scope.data.selectedOption !== 'Select') {
vm.showWarning = true;
$scope.showUpload = true;
vm.showEnvironmentMessage = true;
} else {
vm.showWarning = false;
vm.showEnvironmentMessage = false;
$scope.showUpload = false;
}
};
Currently onload of the page the upload button is hidden, but if I select any other item in dropdown the upload button is not showing. Also, I want to show in the "showEnvironmentMessage" as You have selected Option 1, if the user selects ay1, if they choose ay2, then show message as you have selected option 2 in the "showEnvironmentMessage"label.
How to achieve this? Thanks
I have a group of checkboxes which are populated from,the database. Each checkbox value contains a Book Id.
Now I have a Save button which contains a ng-click="save()" function. How can I pass the value of checked book Ids to the controller, and how can I catch the array of Ids in the controller' save() method. How can I bind the checkbox values, with the controller, here?
Here is the code in the view,
<div ng-repeat="bk in Books">
<input type="checkbox" value={{bk.Id}} ng-checked="$index ===0 ? true : false"/>{{bk.Name}}
</div>
<br/>
<input type="button" id="save" class="btn btn-success" value="Save" ng-click="save()"/>
<br/>
You don't need to pass the Ids values from the view, you can grab them directly within the controller through the binding mechanism.
Here you have a working sample. Whit this you can get rid of value={{bk.Id}} and ng-checked="$index ===0 ? true : false". Also notice how it is initialized the state (checked or unchecked) of the checkbox: ng-init="bookModel[bk.Id] = $first" which sets the checkbox checked only for the first element in the list.
angular
.module('app', [])
.controller('ctrl', function($scope) {
$scope.Books = [{
Id: 1,
Name: "Harry Potrer"
},
{
Id: 2,
Name: "Python"
},
{
Id: 3,
Name: "C#"
}
];
$scope.bookModel = {};
$scope.save = function() {
var checkedBooks = [];
for (var k in $scope.bookModel) {
if ($scope.bookModel.hasOwnProperty(k) && $scope.bookModel[k]) {
checkedBooks.push(k);
}
}
//do your stuff with the ids in `checkedBooks`
alert(checkedBooks);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<span ng-app="app" ng-controller="ctrl">
<div ng-repeat="bk in Books">
<input type="checkbox" ng-model="bookModel[bk.Id]" ng-init="bookModel[bk.Id] = $first"/>{{bk.Name}}
</div><br/>
<input type="button" id="save" class="btn btn-success" value="Save" ng-click="save()" />
</span>
I am trying to add a form to a bootstrap accordion and post a text value. Without accordion the form works alright. When i added the form to the accordion i am not able to pass text box value to angularjs controller. Basically i am not able to perform the CRUD operations as i am not able to pass the values. Minimum code related to the issue.
main.html
<div ng-controller="myController">
<accordion class="accordion" close-others="oneAtATime">
<accordion-group ng-repeat="group in groups" is-open="status.isOpen[$index]" >
<accordion-heading>
{{group.groupTitle}} <i class="fa chevron-icon" ng-class="{'fa-chevron-down': status.isOpen[$index], 'fa-chevron-right': !status.isOpen[$index]}"></i>
</accordion-heading>
<div ng-include="group.templateUrl"></div>
</accordion-group>
</accordion>
</div>
controller.js
App.controller('myController', ['$scope', 'Parts', function($scope, Parts) {
var original = $scope.parts;
$scope.submit = function() {
if ($scope.parts.a_id == null) {
$scope.createNewPart();
} else {
$scope.updatePart();
}
};
$scope.createNewPart = function() {
Parts.resource1.create($scope.parts);
};
$scope.updatePart = function() {
Parts.resource2.update($scope.parts)
};
$scope.oneAtATime = true;
$scope.groups = [{
groupTitle: "ADD 1",
templateUrl: "file1.html"
}, {
groupTitle: "ADD 2",
templateUrl: "file2.html"
}];
$scope.status = {
isOpen: new Array($scope.groups.length)
};
for (var i = 0; i < $scope.status.isOpen.length; i++) {
$scope.status.isOpen[i] = (i === 0);
}
} ]);
file1.html
<div>
<form name="myForm" ng-submit="submit()">
<div class="row">
<label class="col-md-2 control-lable" for="part">Add1</label>
<input type="text" ng-model="parts.part" id="part" required />
</div>
<div class="row">
<input type="submit" value="{{!parts.id ? 'Add' : 'Update'}}" ng-disabled="myForm.$invalid">
</div>
</form>
//inside table
<tr ng-repeat="a in availableparts >
<td>{{a.id}}</td>
<td>{{a.apart}}</td>
<td><button type="button" ng-click="editPart(a.id)" >Edit</button>
<button type="button" ng-click="deletePart(a.id)">Remove</button>
</td>
</tr>
</div>
Initially, you need to set $scope.parts to empty object {}, so when you access $scope.parts.id you will have null, instead of error saying id of undefined
I try to do TODO list using Angular.js
I have function addTask, but it dosen't work. I try to give it project and than get array of tasks. In Chrome I see this mistake "Cannot read property 'tasks' of undefined"
This is my app.js
var app = angular.module('toDoList', ['ngDialog']);
app.controller('MainCtrl', [
'$scope',
'$rootScope',
'ngDialog',
function($scope, $rootScope, ngDialog){
$scope.test = 'Hello world!';
$scope.projects = [
{id: '1', title: 'For Home', tasks: [ {title: 'task 1', done:false }, {title: 'task 2', done: false }]},
{id: '2', title: 'For Work', tasks: [ {title: 'task 1', done:false }, {title: 'task 2', done: false }]}
];
$scope.addProject = function(){
if(!$scope.title || $scope.title === '') { return; }
$scope.projects.push({
title: $scope.title
});
};
$scope.addTask = function(project){
$scope.project.tasks.push({
title: $scope.tasktitle, done: false
});
}]);
This is my html code
<body ng-app="toDoList" ng-controller="MainCtrl">
<div class="project" ng-repeat="project in projects">
<span class="title">{{project.title}}</span>
<form name="frm" ng-submit="addTask(project)">
<input
type="text"
class="form-control"
placeholder="Start to type task"
ng-model="tasktitle">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Add Task</button>
</span>
</form>
<ul class="todo-list">
<li ng-repeat="task in project.tasks">
<input type="checkbox" ng-model="task.done">
<span ng-class="{done:task.done}">{{task.title}}</span>
</li>
</ul>
</div>
</body>
How can I push my data about task?
This is doesn't work:
$scope.addTask = function(project){
$scope.project.tasks.push({
title: $scope.tasktitle, done: false
});
};
First and foremost, you're missing an 's' in $scope.project.tasks.push. It should be projects, so as to match up with the scope variable.
Second, you're not actually choosing a project to push to. $scope.projects is an Array. You would need to choose an index to push to. Here's what I think you're going for:
HTML
<form name="frm" ng-submit="addTask(project.id, tasktitle)">
Javascript
$scope.addTask = function(id, task){
$scope.projects[id].tasks.push({
title: task
, done: false
});
};
EDIT: Working codepen at: codepen.io/Pangolin-/pen/YPVwye – Pangolin
I could fix this, but i don't know is it right?
HTML
<form name="frm" ng-submit="addTask(project.tasks, tasktitle)" class="input-group">
Javascript
$scope.addTask = function(project, value){
project.push({
title: value
});
I have two select i want to enable my button when i have selected values in my two select, i don't know how i can do this ?
JSFIDDLE
My HTML:
<div ng-app="myapp">
<fieldset ng-controller="FirstCtrl">
<select
ng-options="people.first for people in people_1"
ng-model="selectedPerson1"></select>
<select
ng-options="people.first for people in people_2"
ng-model="selectedPerson2"></select>
</fieldset>
<br/><button class="btn btn-primary" ng-click="platform=true">Comparer</button>
</div>
You can do this using the ng-disabled directive. I have amended your jsfiddle here: http://jsfiddle.net/p0t4cw84/3/. I moved the button into the scope of the controller FirstCtrl and added a method on the scope that will return true or false if values are selected.
$scope.enableCompare = function () {
return !($scope.selectedPerson1 && $scope.selectedPerson2);
};
I hope this helps.
Here you go.There are lot of issue in you fiddle.I fixed it.
http://jsfiddle.net/purpz7u3/
<div ng-app="myapp">
<div ng-controller="FirstCtrl">
<fieldset >
<select
ng-options="people.first for people in people_1"
ng-model="selectedPerson1" ng-change="check()"></select>
<select
ng-options="people.first for people in people_2"
ng-model="selectedPerson2" ng-change="check()"></select>
</fieldset>
<br/><button class="btn btn-primary" ng-disabled="checked">Comparer</button>
</div>
</div>
var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
$scope.checked = true;
$scope.people_1 = [
{ id: 1, first: 'John' },
{ id: 2, first: 'Rocky' }
];
$scope.people_2 = [
{ id: 1, first: 'Rambo' },
{ id: 2, first: 'Balboa'}
];
$scope.check = function(){
if($scope.selectedPerson1 && $scope.selectedPerson2){
$scope.checked=false;
}
}
});