Setting Angular variable in HTML whilst using ng-repeat - angularjs

I have the following code which works great:
<div class="progress progress-striped active">
<div class="progress-bar progress-bar-success" style="width: {{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}%;background-color: #5cb85c">
{{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}
</div>
</div>
However, as you can see, I am repeating {{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}} alot and I therefore would like to assign it to angular variable. I would also like to do an NG-switch on the result. This is what I have tried
<div class="progress progress-striped active" ng-init = "barPercentage= {{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}">
<div ng-switch="barPercentage">
<div ng-switch-when=">=0" class="progress-bar progress-bar-success" style="width: {{barPercentage}}%;background-color: #5cb85c">
{{(p.availableIpAddressCount/p.totalIpAddressCount)*100|number:0}}
</div>
</div>
</div>
However this doesn't work at all but I'm unsure why. I get no errors in the console.
Any ideas?

var app = angular.module('demo', []);
app.controller('DemoCtrl', DemoCtrl);
DemoCtrl.$inject = ['$scope'];
function DemoCtrl($scope) {
// you can change this as you like. its just for demo purposes.
$scope.p = {
"availableIpAddressCount": 100,
"totalIpAddressCount": 70
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script data-require="ui-bootstrap-tpls#*" data-semver="1.2.5" src="https://cdn.rawgit.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-1.2.5.js"></script>
<script data-require="jquery#2.2.0" data-semver="2.2.0" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link data-require="bootstrap#3.3.2" data-semver="3.3.2" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script data-require="bootstrap#3.3.2" data-semver="3.3.2" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<script data-require="ui-bootstrap#1.3.2" data-semver="1.3.2" src="https://cdn.rawgit.com/angular-ui/bootstrap/gh-pages/ui-bootstrap-tpls-1.3.2.js"></script>
<div ng-app="demo" ng-controller="DemoCtrl">
<!--using ng-if -->
<div class="progress progress-striped active" ng-init="barPercentage= (p.availableIpAddressCount/p.totalIpAddressCount*100) | number:0">
<div ng-if="barPercentage>=0" class="progress-bar progress-bar-success" style="width: {{barPercentage | number: 0}}%;background-color: #5cb85c">
{{barPercentage |number:0}}
</div>
</div>
<!-- using ng-switch -->
<div class="progress progress-striped active" ng-init="barPercentage= (p.availableIpAddressCount/p.totalIpAddressCount*100) | number:0">
<div ng-switch on="(barPercentage>=0)">
<div ng-switch-when="true">
<div class="progress-bar progress-bar-success" style="width: {{barPercentage | number: 0}}%;background-color: #5cb85c">
{{barPercentage |number:0}}
</div>
</div>
<div ng-switch-default>
<!-- code to render the regular block -->
</div>
</div>
</div>
</div>

I would highly suggest that you create a variable in your controller and expose it to the scope.
$scope.barPercentage = (p.availableIpAddressCount/p.totalIpAddressCount)*100 ;
The way to do it in your html is effectively ng-init, but it's highly unrecommended in the docs.
Note that there are no AngularJS variables, rather a JavaScript object called $scope that is exposed to your template.

Related

Bootstrap Column not working as Expected

I am having a bootstrap div with 3 columns as given in the html below and when i have a select control in the first div of the row, the remaining div's don't show up. but if i comment out the same and check with hello world text. All columns show up just like it should be.
not sure what is that i am missing, hence looking out for a new set of eyes to point out what is wrong. :)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container-fluid">
<div style="text-align:center">
<h1>Wind Monitoring System </h1>
</div>
<div class="row">
<div class="col-md-4" style="background:blue">
<span>Hello World1</span>
<!--<div>
<select id="states" ng-model="selectedState" ng-change="getCities()" size="3" ng-options="state.Name for state in states" />
</div>-->
</div>
<div class="col-md-4" style="background:blue">
<span>Hello World2</span>
</div>
<div class="col-md-4" style="background:blue">
<span>Hello World3</span>
<!--<select id="city" ng-show="selectedState != null" ng-model="selectedCity" size="3" style="height:200px;width:200px" ng-options="city.city for city in cities | filter : {State:selectedState.Name}" />-->
</div>
</div>
</div>
If I'm not mistaken the select tags should be as such <select></select> I believe you are supposed to be binding your options not your <select> tags.
Check this link out
Okay here you go
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<div ng-app="app">
<div class="container-fluid">
<div style="text-align:center">
<h1>Wind Monitoring System </h1>
</div>
<div class="row">
<div class="col-md-4" style="background:blue">
<span>Hello World1</span>
<div ng-controller="Test">
<select ng-model="selectedState" size="3" ng-options="item for item in items"></select>
</div>
</div>
<div class="col-md-4" style="background:blue">
<span>Hello World2</span>
</div>
<div class="col-md-4" style="background:blue">
<span>Hello World3</span>
</div>
</div>
</div>
</div>
JavaScript
var app = angular.module('app',[]);
app.controller('Test',function($scope){
$scope.items = ['one','two','three','four']
});
I believe all I did was add the closing tag.

Two inline Bootstrap datepickers not fitting bootstrap grid column and panel

I am a newbie when it comes to frontend. I have been working on a little single page angularjs app and I came across an issue. On my page, I need two inline datepickers. I chose angular-ui-bootstrap datepickers. They render in an odd way veiling each other:
veiling datepickers
Here's part of my HTML code:
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4>Date range</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-5">
<div style="display: inline-block;">
<uib-datepicker ng-model="dateFrom" starting-day="1" max-date="dateTo" show-weeks="false"></uib-datepicker>
</div>
<div>
<label>Date from {{dateFrom | date: "dd/MM/yyyy"}}</label>
</div>
</div>
<div class="col-md-5 ">
<div style="display: inline-block;">
<uib-datepicker ng-model="dateTo" starting-day="1" min-date="dateFrom" show-weeks="false"></uib-datepicker>
</div>
<div>
<label>Date to {{dateTo | date: "dd/MM/yyyy"}}</label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<label> Additional Info</label>
</div>
Do you have any idea how to make them fit the panel normally without this veiling?
Before I put datepickers into panel div, the display issue was the same. They were tighten together and not fitting the grid column.
Each help will be appreciated
As <uib-datepicker> has a fixed size and cannot be resized according to grid width, you can customize the size of the small buttons (days).
See in the snippet below the .custom-size .btn-sm CSS class:
var app = angular.module('app', ['ui.bootstrap']);
app.controller('MyCtrl', function($scope) {
});
.custom-size .btn-sm {
padding: 4px 8px;
font-size: 11px;
line-height: 1.5;
border-radius: 3px;
}
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>AngularJS UI Bootstrap</title>
<script data-require="angular.js#1.4.x" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
<script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.3.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="app.js"></script>
</head>
<body ng-controller="MyCtrl">
<div class="container-fluid">
<h2>AngularJS: UI Bootstrap datepicker</h2>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4>Date range</h4>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-6">
<div style="display: inline-block;">
<uib-datepicker ng-model="dateFrom" starting-day="1" max-date="dateTo" show-weeks="false" class="custom-size"></uib-datepicker>
</div>
<div>
<label>Date from {{dateFrom | date: "dd/MM/yyyy"}}</label>
</div>
</div>
<div class="col-md-6">
<div style="display: inline-block;">
<uib-datepicker ng-model="dateTo" starting-day="1" min-date="dateFrom" show-weeks="false" class="custom-size"></uib-datepicker>
</div>
<div>
<label>Date to {{dateTo | date: "dd/MM/yyyy"}}</label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<label> Additional Info</label>
</div>
</div>
</div>
</body>
</html>

Knockout Bindings are not being applied when using angular templates

I'm trying to port an angular example to dukescript and navigation behaves as spected, but when tried to apply bindings nothing happened.
I've tried both with the script tag an in external files with the same results and knockoutjs bindings are not being applied.
Extract:
<script>
.
.
.
<span style="float:left;" id="movimientos" data-bind="text:movimientos"></span>
.
.
.
<script>
Full code:
<html ng-app="proyecto" dir="ltr"><head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>Puzzle Logic</title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/estilos.css" rel="stylesheet">
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/app.js"></script>
<script src="js/routes.js"></script>
<script src="js/controllers.js"></script>
<script src="js/controllers/dashboard.js"></script>
</head>
<body contenteditable="true">
<!-- animation="slide-left-right-ios7" class="platform-android platform-cordova platform-webview" -->
<ion-nav-view> </ion-nav-view>
<script id="templates/home.html" type="text/ng-template">
<ion-view>
<ion-content padding="true" class="fondoDePaisaje">
<button class="button button-assertive icon ion-gear-a" style="float:right;"></button>
<br>
<br>
<!--logo-->
<div id="logo" style="text-align:center;">
<img src="img/logo_pusle.png" alt="Puzzle Land" width="300rem">
<div id="botones_comienzo">
<a href="#/dashboard" class="opcion_menu">
<span>J</span>
<span>u</span>
<span>g</span>
<span>a</span>
<span>r</span>
</a>
<br>
<br>
<a href="#/score" class="opcion_menu">
<span>S</span>
<span>c</span>
<span>o</span>
<span>r</span>
<span>e</span>
</a>
<br>
<br>
<a href="" class="opcion_menu">
<span>A</span>
<span>y</span>
<span>u</span>
<span>d</span>
<span>a</span>
</a>
</div>
</div>
<br>
<br>
<br>
<br>
<!--pie de pagina-->
<div class="row-center" style="background-color:transparent; text-align: center;">
<div>Derechos Reservados</div>
</div>
</ion-content>
</ion-view>
</script>
<script id="templates/dashboard.html" type="text/ng-template">
<ion-view>
<!-- ng-controller="CntrlDashboard" -->
<ion-content class="fondoDePaisaje">
<!--movimientos realizados-->
<div class="mismovimientos">
<span style="float:left;">Mov:</span>
<span style="float:left;" id="movimientos" data-bind="text:movimientos"></span>
Nuevo
<!--cronometro-->
<span style="float:right; margin-rigth:10px; padding-right:20px;">T:<span id="minutos">0</span>:<span id="segundos">0</span></span>
<div style="clear:both;"></div>
</div>
<!--crearemos la tabla-->
<div class="row tabla" id="tabla1">
<div class="col" posicion="0" id="p1" ng-click="mensaje( $event )">1</div>
<div class="col" posicion="1" id="p2" ng-click="mensaje( $event )">2</div>
<div class="col" posicion="2" id="p3" ng-click="mensaje( $event )">3</div>
<div class="col" posicion="3" id="p4" ng-click="mensaje( $event )">4</div>
</div>
<div class="row tabla" id="tabla2">
<div class="col" posicion="0" id="p5" ng-click="mensaje( $event )">5</div>
<div class="col" posicion="1" id="p6" ng-click="mensaje( $event )">6</div>
<div class="col" posicion="2" id="p7" ng-click="mensaje( $event )">7</div>
<div class="col" posicion="3" id="p8" ng-click="mensaje( $event )">8</div>
</div>
<div class="row tabla" id="tabla3">
<div class="col" posicion="0" id="p9" ng-click="mensaje( $event )">9</div>
<div class="col" posicion="1" id="p10" ng-click="mensaje( $event )">10</div>
<div class="col" posicion="2" id="p11" ng-click="mensaje( $event )">11</div>
<div class="col" posicion="3" id="p12" ng-click="mensaje( $event )">12</div>
</div>
<div class="row tabla" id="tabla4">
<div class="col" posicion="0" id="p13" ng-click="mensaje( $event )">13</div>
<div class="col" posicion="1" id="p14" ng-click="mensaje( $event )">14</div>
<div class="col" posicion="2" id="p15" ng-click="mensaje( $event )">15</div>
<div class="col vacio" posicion="3" id="p16" ng-click="mensaje( $event )"></div>
</div>
<!--volver-->
<div class="mismovimientos">
<span style="float:left; font-size:16px !important;">Derechos Reservados</span>
Volver
<div style="clear:both;"></div>
</div>
</ion-content><!--cierre del contenido-->
</ion-view>
</script>
</body></html>
How can I fix it?
Update
Did a test on dew and the results are that bindings are not applied inside the script tag.
Test
Java:
package dew.demo.ko4j;
import net.java.html.json.*;
#Model(className="Hello", properties={
#Property(name="say", type=String.class)
})
class HelloViaKO {
static {
Hello model = new Hello("Hello World!");
model.applyBindings();
}
}
HTML:
<h1>Hello</h1>
Test 1!
<div>
<span style="float:left;" id="moves" data-bind="text: say">vxc</span>
</div>
Test2!
<script>
<span style="float:left;" id="moves" data-bind="text: say">vxc</span>
</script>
Test3!
<script>
<span style="float:left;" id="moves">hello</span>
</script>
The generated HTML is
and the highighted part should be:
<script>
<span style="float:left;" id="moves" data-bind="text: say">Hello World!</span>
</script>
You've switched to knockout for binding, so I'll update my answer:
Your DEW example tries to show that bindings are not applied inside script tags. But script tags are not rendered in the UI, so even if the bindings would be applied in your example, you wouldn't see it in the output.
Despite that, your assumption that they are not applied is correct. This is the expected and correct behavior. In Knockout, script tags can be used to define templates:
http://knockoutjs.com/documentation/template-binding.html
In that case the template binding takes the active template script, inserts it at the chosen location and only then applies the bindings.

How to use a dropdown bar and ng-view

I've looked at ng-view according to
https://docs.angularjs.org/api/ngRoute/directive/ngView
However, I'm very unsure as to how I would incorporate the above with the dropdown menu.
Here's a few screenshots showing what the website currently looks like for reference.
imgur.com/a/NUVEe
Here's the code
index.html
<html ng-app="app">
<head>
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body ng-controller="MainController">
<nav class="navbar navbar-default">
<div class="container"> <!-- top intro part -->
<div class="navbar-header">
<a class="navbar-brand" href="#/"> OPENCV 3.0.0</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><i class="fa fa-home"></i> Home</li>
<li><i class="fa fa-shield"></i> About</li>
<li><i class="fa fa-comment"></i> Contact</li>
</ul>
</div>
</nav>
<ng-view>
<div class="row"> <!-- Dropdown menu -->
<div class="col-md-20">
<div id="main">
<form class="form-horizontal" role="form">
<label class="control-label col-md-2">Filter List:</label>
<div class="col-md-5">
<select class="form-control"
ng-options="filter for filter in filters"
ng-model="filter"
ng-change="GoToOpenCVpage(filter)">
<option value=""> Select Filter</option>
</select>
</div>
</form>
</div>
</div>
</div>
</ng-view>
<div id = "content"> <!-- stuff showing opencv filter goes in here -->
<ng-view>
</ng-view>
</div>
<script src="js/angular.min.js"></script>
<script src="js/ui-bootstrap-tpls-0.14.3.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
makegray.html (the html content I want to inject into index.html)
<html ng-app="app">
<head>
<link rel="stylesheet" href="../css/bootstrap.min.css" />
</head>
<body ng-controller="MainController">
<nav class="navbar navbar-default">
<div class="container"> <!-- top intro part -->
<div class="navbar-header">
<a class="navbar-brand" href="#/"> MAKE GRAY</a>
</div>
</div>
</nav>
<form action="../php/makegray.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<script src="js/angular.min.js"></script>
<script src="js/ui-bootstrap-tpls-0.14.3.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
angularJS
var app = angular.module("app", ["ui.bootstrap"]);
/*app.factory("API", function($http) {
return {
getFilters: function(callback) {
$http.get("filters.php").success(callback);
}
}
});*/
app.controller("MainController", function($scope) {
$scope.ListOfOpenCV = {}; // declare array
// $scope.ListOfOpenCV.filter = "";
$scope.filters = ["MakeGray", "Sobel"];
//$scope.ListOfOpenCV.filter = $scope.filters[0];
$scope.GoToOpenCVpage = function(filter){
if(filter === "MakeGray"){
// window.location("pages/Canny.html");
window.open("pages/MakeGray.html", "_blank","height = 400, width = 700");
}
};
});

Angular UI-Bootstrap - Collapse function not working

I have been following the docs for ui-bootstrap. And in the section(ui.bootstrap.collapse) they talk about making a collapse function for content when you click a button.
But I cannot seem to make the Collapse seem to work in my code.
What am I missing or doing wrong?
I have looked at other Stacks and have seen that other people use anchor tags instead of button tags. So I don't think that is the issue.
Index HTML
<!DOCTYPE html>
<html lang="en" data-ng-app="app">
<head>
<meta charset="UTF-8">
<meta name="description" content="stuff">
<meta name="keywords" content="stuff">
<meta name="author" content="stuff">
<title> Title</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="lib/bootstrap/dist/css/bootstrap.css">
<!-- Custom styles -->
<link href="css/style.css" rel="stylesheet">
<link href="css/svg_style.css" rel="stylesheet">
<!--Jquery -->
<script src="lib/jquery/dist/jquery.min.js"></script>
<!-- Angular -->
<script src="lib/angular/angular.min.js"></script>
<script src="lib/angular-route/angular-route.min.js"></script>
<script src="lib/angular-animate/angular-animate.min.js"></script>
<script src="lib/angular-cookies/angular-cookies.min.js"></script>
<!-- Bootstrap -->
<script src="lib/angular-bootstrap/ui-bootstrap.min.js"></script>
<script src="lib/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>
</head>
<body id="index_body">
<div data-ng-controller="HeaderCtrl">
<div class="top-header" data-ng-include="templateUrl"></div>
</div>
<div class="page [[ pageClass ]]" ng-view autoscroll="true"></div>
<!-- Main JS -->
<script src="js/app.js"></script>
<!-- Controllers -->
<script src="js/controllers/headerCtrl.js"></script>
<script src="js/controllers/modal.js"></script>
<script src="js/controllers/ResonanceCtrl.js"></script>
<script src="js/controllers/ContactCtrl.js"></script>
<script src="js/controllers/LandingCtrl.js"></script>
<script src="js/controllers/SignInCtrl.js"></script>
<!-- Directives -->
<!-- <script src="js/directives/LandingAnimation.js"></script> -->
<script src="js/jq.js"></script>
</body>
</html>
Landing Page HTML
<div class="col-xs-12 col-sm-12 col-md-5">
<div class="caption">
<h1 class="text-left h-color thin">
Text Header
</h1>
<p class="lead p-color">More Text</p>
<!-- Here is my Toggle Button --> <a class="lead p-color learn-button togglebtn shake shake-rotate" data-ng-click="isCollapsed = !isCollapsed">
<small>
<i class="glyphicon" data-ng-class="{'glyphicon-minus': status.open, 'glyphicon-plus': !status.open}"></i> Learn More
</small>
</a>
</div>
</div>
<div class="hidden-xs hidden-sm col-md-7 col-lg-offset-1 col-lg-6">
<img alt="Image" class="img-responsive center-block" src="images/kip-animation.png" />
</div>
<!--Here is the what I want to collapse -->
<div id="myContent" collapse="isCollapsed" class="row row-offset row-pad" style="margin: 0 30px">
<div class="col-xs-6 col-sm-4 col-md-4">
<div class="lead caption text-center">
<h3 class="h-color2">Item 1</h3>
</div>
<div class="thumbnail">
<img style="height: 100px; width: auto;" class="img-circle" src="images/logo-bunny.png" alt="Logo">
</div>
<div class="lead caption">
<p class="p-color"><small>Text</small>
</p>
</div>
</div>
<div class="col-xs-6 col-sm-4 col-md-4">
<div class="lead caption text-center">
<h3 class="h-color2">Item 2</h3>
</div>
<div class="thumbnail">
<img style="height: 100px; width: auto;" class="img-circle" src="images/logo-bunny.png" alt="Logo">
</div>
<div class="lead caption">
<p class="p-color"><small>Text</small>
</p>
</div>
</div>
<div class="col-xs-6 col-sm-4 col-md-4">
<div class="lead caption text-center">
<h3 class="h-color2">Item 3</h3>
</div>
<div class="thumbnail">
<img style="height: 100px; width: auto;" class="img-circle" src="images/logo-bunny.png" alt="Logo">
</div>
<div class="lead caption">
<p class="p-color"> <small>Some Text</small>
</p>
</div>
</div>
</div>
<!-- END DROPDOWN-->
App Javascript
var app = angular.module('app', ['ui.bootstrap', 'ngRoute', 'ngAnimate']);
app.config(function($interpolateProvider, $routeProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
$routeProvider
.when('/', {
templateUrl : 'pages/LandingPage.html',
controller : 'LandingCtrl'
})
.otherwise({ redirectTo: '/signin'});
});
Controller Javascript
app.controller('LandingCtrl', function($scope) { // jshint ignore:line
$scope.pageClass = 'page-landing';
$scope.isCollapsed = true;
});
I solved the issue. I was using Jquery before to toggle the display either hidden or shown.
In my Css I had:
myContent {
display: none;
}
Once I deleted that. It worked perfectly fine.

Resources