Removing ng-class - angularjs

How can I remove ng-class in angular js.
Here is my example code
<form name="myform" ng-class="plus">
//...
<button ng-click="save()">click</button>
</form>
and what I tried but does not work
$scope.add_page = function(){
$scope.myform.removeClass(plus);//what i try but not work
}
any suggest how to removed class or remove attribute ?

Just change it like this
<form name="myform" ng-class="{plus?'plus':''}" ng-init="plus=true">
//...
<button ng-click="save()">click</button>
</form>
and then in controller
$scope.plus=true;
$scope.add_page = function(){
$scope.plus=false;//what i try but not work
}

You can try the below method
<form name="myform" ng-app="myApp" ng-controller="myCtrl">
<div ng-class="plus">with class </div>
<button ng-click="clickme()">
click me to remove class
</button>
</form>
Js
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.plus="plus";
$scope.clickme=function(){
$scope.plus="";
}
});
CSS
.plus{
background-color:red;
}
Working demo
Note :
You can do the same thing using other methods also.
1) Simply create a class using class instead of ng-class. ng-class is not required here, and then remove it by getting the particular element using id or via event
2) Angularjs is maid on top of javascript only so you can use any of the javascript syntax in angularjs(not a good practice)to remove class .

Related

How to disable a button in ANGULARJS

i have angularJS 1.5.0-rc.2
I found on the internet that i can use the directive
data-ng-disabled="Expression"
But it won't disable a button.
What i tried :
<button data-ng-disabled="false">Select</button> // don't work
<button data-ng-disabled="loaded">Select</button> // don't work
<button data-ng-disabled="loaded != false">Select</button> // don't work
Loaded is defined in my controller as $scope.loaded = false.
How can i disable a button ? Thanks
The button will be disabled only if the expression is true.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-init="disableBtn=true">
<button ng-disabled="disableBtn">test</button>
</div>
I make it without derective. I made it like this:
remove
In controller first of all made
$scope.disabled = true;
Than in if/else remove class disabled and change $scope.disabled = false;
I give you example with tag <a></a> this work with button simple.
And in button ng-click first made Test ng-click="disabled||usertaskctrl.removeTasksToArchive()" before make click on some you function.
Try this
1 This is direct method of implementation
<script>src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<button ng-disabled=true>test</button>
</div>
2 another way you can change the value for
<div ng-app>
<button ng-disabled=dsblBtn>test</button>
</div>
in controller inside init function
$scope.dsblBtn = true;
Always use the button tag with ng-disabled in angular js otherwise it will not work.

AngularJS invoke Controller function

sorry for the rather basic question but I am really a beginner developing in AngularJS.
So I have a conteoller like this (like explaned here: https://github.com/johnpapa/angular-styleguide):
(function() {
'use strict';
angular
.module('project.management')
.controller('ManagementController', ManagementController);
function ManagementController() {
var vm = this;
vm.getUsersBySearchString= getUsersBySearchString;
////////////
function getUsersBySearchString(searchString) {
alert('get User By searchstring: ' + searchString);
}
};
})();
Now I have a HTML fragment in my template and I really don't know how to invoke function getUsersBySearchString(searchString). I have tried this one:
<div ng-controller="vm">
<form class="well form-search">
<label>Usersuche:</label>
<input type="text" ng-model="term" class="input-medium search-query" placeholder="Username">
<button type="submit" class="btn" ng-click="getUsersBySearchStringgetUsersBySearchString()">Suchen</button>
</form>
<pre ng-model="result">
{{result}}
</pre>
</div>
but I don't know what to put here
<div ng-controller="vm">
and how to invoke the method.
Thanks a lot for help!
<div ng-controller="vm">
That is incorrect. You have no controller named "vm". Your controller is named ManagementController.
The syntax for your use-case is
<div ng-controller="ManagementController as vm">
And to invoke the function, you would use
ng-click="vm.getUsersBySearchString(term)"
Note that the alias you choose in the HTML has no relationship with the alias you chose for thisin the controller code. You might very well use
<div ng-controller="ManagementController as managementCtrl">
and
ng-click="managementCtrl.getUsersBySearchString(term)"

Why is my ng-if not working? It's not hiding the div

In my Angular app, I want a certain div to appear if a variable is true, and to disappear if it is false.
However, it is not working. See my Fiddle
Can anyone help me understand why?
HTML
<div ng-controller="MyCtrl">
<div id="newProjectButton" class="project" ng-click="newProjectActive()" ng-if="!creatingNew">
<h1> + </h1>
<h3> New Project </h3>
</div>
<div id="newProjectActive" class="project" ng-if="creatingNew">
<form>
<input name="name" ng-model="newProjectName" type="text"></input>
<button ng-click="newProject()" type="submit" class='btn btn-primary'>Save</button>
</form>
</div>
</div>
JS
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.name = 'Superhero';
$scope.creatingNew = false;
$scope.newProjectActive = function () {
$scope.creatingNew = true;
}
$scope.newProject = function () {
alert($scope.newProjectName);
}
}
your angular version is 1.0.1. directive ng-if is not in this version of angular.
its introduce in angular 1.1.5
check this article
and
check it in angular under Directives topic change log
AngularJS first added the ngIf directive in 1.1.5
please update the angular version
here is the Demo
your controller should be like
myApp.controller("MyCtrl" , function($scope) {
because the global controllers are not supported by default in angular 1.3... check this one
First, when I looked at your fiddle, there was older version of your example there,
Second, which actually may be a reason, is in that example you were using angular in version 1.0.1, and i believe that version didn't implement ng-if. Updating to latest version will fix your problem

how to catch button click event using AngularJs and show an alert box

How can we trigger the button click event using AngularJs and show an alert message box? I have tried the alert("message") messagebox but it doesn't work.
You can use ng-click on button something like this:
Controller :
var VLogin = angular.module('myApp',[]);
VLogin.controller('TestCtrl', ['$scope',function($scope) {
$scope.clicked = function(){
alert("Clicked");
}
}]);
And in your HTML like :
<div ng-app="myApp">
<div ng-controller="TestCtrl">
<button ng-click="clicked()">Click me!</button>
</div>
</div>
Here is the working Fiddle :- http://jsfiddle.net/nuejh9h6/
Thanks
we need to see what you have wrote but in general this is how ng-click works https://docs.angularjs.org/api/ng/directive/ngClick and you can write your alert code inside or in a new method and call it.

Get Form Action in a Controller

Hello currently I have a html form like that:
<div ng-controller="DemoCtrl">
<form name="myForm" action="/a/url">
....
</form>
<button ng-click="next(myForm)">Klick me</button>
</div>
is there a way to access the form action ?
what i suggest you not to provide action name in the form, instead of that within ng-click action javascript method in JS file either use $http service or $window service.
This can happen with the following htmlcode:
<div ng-controller="DemoCtrl">
<button ng-click="next(myForm)">Klick me</button>
</div>
JS Code:
$scope.next = function (formObject) {
// whatever you want to do
}
$scope will pick all the object defined within form tag.
I am also new in ng, if anything wrong here please correct me.

Resources