How to bind (?) several variables in a complex way in AngularJS? - javascript

Suppose some widget like range slider binds to a 2-element array. Also I want to display range limits in separate text fields. How to accomplish that?
angular
.module('myApp', ['ui.slider']);
angular.module('myApp')
.controller('MyController', MyController);
function MyController() {
var vm = this;
vm.slider = [5,15];
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://angular-ui.github.io/ui-slider/src/slider.js"></script>
<body ng-app="myApp">
<div ng-controller="MyController as vm">
<strong>Range slider:</strong>
<div ui-slider="{range: true}" min="1" max="20" step="1" ng-model="vm.slider"></div>
<strong>It's value is bound as an array:</strong><br>
<input type="text" ng-model="vm.slider" /><br>
<strong>How to bind sepately:</strong><br>
From: <input type="text" ng-model="vm.sliderfrom" /> To: <input type="text" ng-model="vm.sliderto" /><br>
</div>
</body>
I need to update array on each limit change and reverse. How to accomplish that?

You can directly bind with array elements
From: <input type="text" ng-model="vm.slider[0]" />
To: <input type="text" ng-model="vm.slider[1]" />
angular
.module('myApp', ['ui.slider']);
angular.module('myApp')
.controller('MyController', MyController);
function MyController() {
var vm = this;
vm.slider = [5, 15];
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://angular-ui.github.io/ui-slider/src/slider.js"></script>
<body ng-app="myApp">
<div ng-controller="MyController as vm">
<strong>Range slider:</strong>
<div ui-slider="{range: true}" min="1" max="20" step="1" ng-model="vm.slider"></div>
<strong>It's value is bound as an array:</strong>
<br>
<input type="text" ng-model="vm.slider" />
<br>
<strong>How to bind sepately:</strong>
<br>From:
<input type="text" ng-model="vm.slider[0]" />To:
<input type="text" ng-model="vm.slider[1]" />
<br>
</div>
</body>

Related

ngRoute is not working in AngularJS

I am trying to use Route in my newEvent page. But it's giving me 404 error. I checked my code but I am not able to identify the error.
can anyone please check my codes and let me know whats wrong im doing here. I uploaded app.js , EditEventController.js , index.html and NewEvent.html file. I also uploaded a screenshot. Please take a look and let me solve this error.
Thank you
'use strict';
var eventsApp = angular.module('eventsApp', ['ngResource', 'ngRoute'])
.config(function ($routeProvider) {
$routeProvider.when('/newEvent', {
templateUrl: 'templates/NewEvent.html',
controller: 'EditEventController'
});
});
.factory('myCache', function ($cacheFactory) {
return $cacheFactory('myCache', {
capacity: 3
});
});
........................
'use strict';
eventsApp.controller('EditEventController',
function EditEventController($scope, eventData) {
$scope.saveEvent = function (event, newEventForm) {
if (newEventForm.$valid) {
eventData.save(event)
.$promise
.then(
function (response) {
console.log('success', response)
})
.catch(
function (response) {
console.log('failure', response)
}
);
}
};
$scope.cancelEdit = function () {
window.location = "/EventDetails.html";
};
}
);
........................
<!DOCTYPE html>
<html lang="en" ng-app="eventsApp">
<head>
<meta charset="utf-8">
<title>Event Registration</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/app.css">
<script src="lib/jquery.min.js"></script>
<script src="lib/underscore-1.4.4.min.js"></script>
<script src="lib/bootstrap.min.js"></script>
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-sanitize.js"></script>
<script src="lib/angular/angular-resource.min.js"></script>
<script src="angular/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/EventController.js"></script>
<script src="js/controllers/EditEventController.js"></script>
<script src="js/services/EventData.js"></script>
<script src="js/filters.js"></script>
</head>
<body>
<div class="container">
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li> Create Event </li>
</ul>
</div>
</div>
<ng-view> </ng-view>
</div>
</body>
</html>
...................................
<div style="padding-left:20px; padding-right:20px">
<div class="container">
<h1> New Event</h1>
<hr />
<form name="newEventForm">
<fieldset>
<label for="eventName">Event Name: </label>
<input id="eventName" type="text" required ng-model='event.name' placeholder="Name of your event...." />
<label for="eventDate">Event Date: </label>
<input id="eventDate" type="text" required ng-pattern="/\d\d/\d\d/\d\d\d\d/" ng-model='event.date' placeholder="format (mm/dd/yyyy)...." />
<label for="eventTime">Event Time: </label>
<input id="eventTime" type="text" ng-model='event.time' placeholder="Start and end time...." />
<label for="eventLocation">Event Location: </label>
<input id="eventLocation" type="text" ng-model='event.location.address' placeholder="Address of event...." />
<br>
<input id="eventCity" type="text" ng-model='event.location.city' class="input-small" placeholder="City ...." />
<input id="eventProvince" type="text" ng-model='event.location.province' class="input-small" placeholder="Province ...." />
<label for="eventImageUrl">Image</label>
<input id="eventImageUrl" type="url" ng-model='event.imageUrl' class="input-xlarge" placeholder="Url of image ...." />
</fieldset>
<img ng-src="{{event.imageUrl}}" src="" />
<br>
<br>
<button type="submit" ng-disabled="newEventForm.$invalid" ng-click="saveEvent(event, newEventForm)" class="btn btn-primary">Save</button>
<button type="submit" ng-click="cancelEdit()" class="btn btn-primary">Cancel</button>
</form>
</div>
</div>
In your html, should this entry:
<script src="angular/angular-route.js"></script>
... be changed to this, so that it matches the location to the other angular files?
<script src="lib/angular/angular-route.js"></script>

Auto Null Value on AngularJS

This is a problem that can be easily done by declaring a scope function but I was wondering if there is a better way to make this easier
The code is:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
<body ng-controller="myCtrl">
<input type="checkbox" ng-model="activate" id="activate"> ACTIVATE <br />
<input type="text" ng-model="first" ng-disabled="!activate">
<input type="text" ng-model="second" ng-disabled="!activate">
<!-- <span id="clickMe">Click Me!</span> -->
<script type="text/javascript">
app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout){
});
</script>
</body>
</html>
So this is the case. The other two fields will be enabled if the checkbox is checked and true and will be disabled if the checkbox is unchecked. What I want is, to make the fields go null or blank automatically upon disabling.
Here is an example: http://plnkr.co/edit/2EPuhRiR9OncV8z7q018?p=preview
Ignoring the fact that it is not a good practice to put too much logic directly in the view, a technical solution would be:
<input type="checkbox" ng-model="activate" id="activate" ng-change="sameAsAbove(first, second); value = null"> ACTIVATE <br />
<input type="text" ng-model="value.first" ng-disabled="!activate">
<input type="text" ng-model="value.second" ng-disabled="!activate">

Change Property on click angularjs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to change a radio button's property on a click on a <div>.
In the below code, the div has a class and I have to change the radio button's property to true if the div is clicked.
I wrote this event handling using jQuery.
Can you tell me how to solve this using Angularjs?
var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope) {
//How to write code here in Angular//
});
<!DOCTYPE html>
<html ng-app='myapp'>
<head>
<script data-require="jquery#*" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script data-require="angular.js#1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
<div>
<input ng-click="Large()" id="large" type="radio" name="button">Large
<input ng-click="Medium()" id="medium" type="radio" name="button">medium
<input ng-click="Small()" id="small" type="radio" name="button">Small
</div>
<div>
<div class="col-xs-12 field">first //click</div><br/>
<div class="col-xs-6 field">second //click</div><br/>
<div class="col-xs-4 field">third //click</div>
</div>
<script>
$(document).ready(function () {
$('.field').bind('click', function () {
if ($(this).hasClass('col-xs-12')) {
$('#large').prop("checked", true);;
}
else if ($(this).hasClass('col-xs-6')) {
$('#medium').prop("checked", true);
}
else
$('#small').prop("checked", true);
})
});
</script>
</body>
</html>
(also available at https://plnkr.co/edit/PNabPp2OvYAS76p3KfsH?p=preview)
Update the model value using ng-click directive
Try this:
var app = angular.module('myapp', []);
app.controller('MainCtrl', function($scope) {
$scope.val = ''
$scope.clickHadler = function(val) {
$scope.val = val;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app='myapp' ng-controller="MainCtrl">
<div>
<input ng-click="Large()" ng-model='val' value='first' id="large" type="radio" name="button">Large
<input ng-click="Medium()" ng-model='val' value='second' id="medium" type="radio" name="button">medium
<input ng-click="Small()" ng-model='val' value='third' id="small" type="radio" name="button">Small
</div>
<div>
<div class="col-xs-12" ng-click='clickHadler("first")'>first //click</div>
<br/>
<div class="col-xs-6" ng-click='clickHadler("second")'>second //click</div>
<br/>
<div class="col-xs-4" ng-click='clickHadler("third")'>third //click</div>
</div>
</body>
Since you can't use label, the angular solution will be is to use model values
var app = angular.module('myapp', [], function() {})
app.controller('MainCtrl', function($scope) {
$scope.fieldClicked = function(size) {
if (size == 'col-xs-12') {
$scope.size = 'large';
} else if (size == 'col-xs-6') {
$scope.size = 'medium';
} else if (size == 'col-xs-4') {
$scope.size = 'small';
}
}
})
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
<div ng-controller="MainCtrl">
<div>
<input ng-model="size" value="large" type="radio" name="button">Large
<input ng-model="size" value="medium" type="radio" name="button">medium
<input ng-model="size" value="small" type="radio" name="button">Small
</div>
<div>
<!--ng-click="fieldClicked(div.size)" in template-->
<div class="col-xs-12 field" ng-click="fieldClicked('col-xs-12')">first</div>
<br/>
<div class="col-xs-6 field" ng-click="fieldClicked('col-xs-6')">second</div>
<br/>
<div class="col-xs-4 field" ng-click="fieldClicked('col-xs-4')">third</div>
</div>
</div>
</div>
No need of script here, just use label instead of div
var app = angular.module('myapp', [], function() {})
app.controller('MainCtrl', function($scope) {
})
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
<div ng-controller="MainCtrl">
<div>
<input ng-click="Large()" id="large" type="radio" name="button">Large
<input ng-click="Medium()" id="medium" type="radio" name="button">medium
<input ng-click="Small()" id="small" type="radio" name="button">Small
</div>
<div>
<label for="large" class="col-xs-12 field">first</label>
<br/>
<label for="medium" class="col-xs-6 field">second</label>
<br/>
<label for="small" class="col-xs-4 field">third</label>
</div>
</div>
</div>

Enable and Disable in angularjs

I try to make disable second field until I do not enter 'Lokesh' in first field for that I have tried below code I am in learning stage of angularjs so please correct me where I am doing mistake.
eg.code
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>
<p>Enter 'Lokesh' to display last name</p>
<div ng-app="myApp" ng-controller="myCtrl">
<input data-ng-model="firstName"
type="text"
ng-disabled="{if(firstName="lokesh")}"
Last Name: <input type="text" ng-model="lastName"><br>
/>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.lastName= "Kumar Gaurav";
});
</script>
</body>
The problem is with the attribute ng-disabled="{if(firstName=" lokesh ")}"
There is no need of using if, you can directly compare the strings.
Update the code as
ng-disabled="firstName=='lokesh'"
Demo
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app>
<input data-ng-model="firstName" class="span12 editEmail" type="text" placeholder="Type lokesh here" />Last Name:
<input type="text" ng-model="lastName" ng-disabled="firstName!=='lokesh'" />
<br>{{firstName}}
</div>

Angular controller not working

I'm newbie to angular
My angular controller is:
var chatApp = angular.module('chatApp.controllers', []);
chatApp.controller('loginController', ['$scope', function($scope) {
$scope.user_name="name";
$scope.user_password="name";
}]);
the app.js is:
'use strict';
angular.module('chatApp', [
'ui.router',
'chatApp.filters',
'chatApp.services',
'chatApp.directives',
'chatApp.controllers'
]).config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('login', {
url: "/login",
templateUrl: "partials/login.html",
controller : "loginController"
})
});
login.html is:
<div>
<div id="wrapper">
<form name="login-form" class="login-form" action="" method="post">
<div class="header">
<h1>Login Form</h1>
</div>
<div class="content">
<input name="username" type="text" class="input username" placeholder="{{user_name}}" />
<div class="user-icon"></div>
<input name="password" type="password" class="input password" placeholder="{{user_password}}" />
<div class="pass-icon"></div>
</div>
</form>
</div>
I've used stateprovider for routing.
But, it's not showing anything in placeholder. What is the reason?
Solved the problem using ng-model instead of {{}}.
<div class="content">
<input name="username" type="text" class="input username" placeholder="username" ng-model="user_name" />
<div class="user-icon"></div>
<input name="password" type="password" class="input password" placeholder="password" ng-model="user_password"/>
<div class="pass-icon"></div>
</div>
This works for me if I say ng-controller = "loginController" in the html-
<div ng-controller = 'loginController'>
<div id="wrapper">
<form name="login-form" class="login-form" action="" method="post">
<div class="header">
<h1>Login Form</h1>
</div>
<div class="content">
<input name="username" type="text" class="input username" placeholder="{{user_name}}" />
<div class="user-icon"></div>
<input name="password" type="password" class="input password" placeholder="{{user_password}}" />
<div class="pass-icon"></div>
</div>
</div>
Here's a JSBin with it working: http://jsbin.com/aDuJIku/265/edit?html,js,output
This solution doesn't include ui-router, but it should work with it.
Try this:
var chatApp = angular.module('chatApp.controllers', []);
chatApp.controller('loginController', ['$scope','$timeout', function($scope,$timeout) {
$timeout(function(){
$scope.user_name="name";
$scope.user_password="name";
},100);
}]);
you just have to change to RouteProvider, In your case you just do:
angular.module('chatApp', [ 'ngRoute' 'ui.router','chatApp.filters','chatApp.services', 'chatApp.directives', 'chatApp.controllers'])
.config(function($routeProvider) {
$routeProvider.when('/login', { templateUrl: "partials/login.html", controller: "loginController" })
});
please make sure that you have angular-route js included.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.11/angular-route.js"></script>
Hope this help you! ;)
Here's some examples for you this
Below mentioned code works.
Note: If somebody tries to run this code in preview its not going to work. JS and HTML code needs to be copied in separate file and use the names mentioned.
var myContrlApp = angular.module('ContrlApp', []);
myContrlApp.controller('MainController', function ($scope) {
var person = {
firstName: "Keith",
lastName: "Jackson",
};
$scope.message = "Welcome, Angular !";
$scope.person = person;
});
<html ng-app="ContrlApp">
<head>
<script type="text/javascript" src="Scripts/angular.js"></script>
<script type="text/javascript" src="Scripts/angular-route.js"></script>
<script type="text/javascript" src="Scripts/script.js"></script>
<!--<link rel="stylesheet" href="style.css" />-->
<title>Angular JS Tryout</title>
</head>
<body ng-controller="MainController">
<div>
<h1>{{message}}</h1>
<div>
<div>First name: {{person.firstName}}</div>
<div>Last name: {{person.lastName}}</div>
</div>
</div>
</body>
</html>

Categories

Resources