i have input with default value 0 , but this value not getting
my js getting error undefined ,if i enter any value it is showing .
but i want to show default value
<tr ng-repeat="r in rvm" >
<td>
<input type="text" lass="input-large" ng-value="2.00" ng-model="rvm.val1" name="val1" />
</td>
<td>
<button type="button" class="btn btn-default" ng-click="addRow1()">Add</button>
</td>
</tr>
JS
var ReceiptsApp = angular.module('ReceiptsApp', []);
ReceiptsApp.controller('ReceiptsController', function ($scope) {
$scope.rvm = [{}];
$scope.addRow1 = function ( ) {
alert($scope.rvm.val1);
}
});
js bin link here
enter link description here
You can either set it inside your controller
ReceiptsApp.controller('ReceiptsController', function ($scope) {
$scope.rvm.val1 = '0.00';
});
or using ng-init
<input type="text" lass="input-large" ng-init="rvm.val1='0.00'" ng-model="rvm.val1" name="val1" />
Just set a default value in the controller:
ReceiptsApp.controller('ReceiptsController', function ($scope) {
$scope.rvm.val1 = 'your default value';
});
If you read this link about ngValue you will see that it's not supposed to be used with a normal input (when you've also set ng-model on it), but rather for radio inputs and option elements. Your best solution is setting the value in the controller, as you should do.
It can also be used to achieve one-way binding of a given expression to an input element such as an input[text] or a textarea, when that element does not use ngModel.
<input type="text" lass="input-large" ng-value="2.00" ng-model="rvm.val1" name="val1" />
Problem is you are using rvm.val1 where rvm is array not an object in array
Change ng-model="rvm.val1" to ng-model="r.val1"
then you will be able to keep track of each value individually.
Inorder to initialize a value
<tr ng-repeat="r in rvm" ng-init="r.val1= 2.00">
Then in
$scope.addRow1 = function (index) {
// access individual object in rvm..
alert($scope.rvm[index].val1);
// push a new object
$scope.rvm.push({});
}
JSBin
If you don't want to use ng-init
You can do this
Each time you push a new item initialize the val1
$scope.rvm.push({val1: 2.00});
JsBin
var ReceiptsApp = angular.module('ReceiptsApp', []);
ReceiptsApp.controller('ReceiptsController', function ($scope) {
$scope.rvm = [{}];
$scope.rvm.val1=0;
$scope.addRow1 = function ( ) {
if($scope.rvm.val1 == ''){
$scope.rvm.val1=0;
alert($scope.rvm.val1);
} else {
alert($scope.rvm.val1);
}
}
});
Hi i have took a if condition where i am checking if input field is blank then it will initialise $scope.rvm.val1=0; and showing in alert.
it will work
Related
I have this html:
<div ng-controller="My.MarkdownEditorController">
<a target="_default" ng-click="changeValue()" class="btn btn-info">Copy Organisation Address to Member</a>
</div>
The Angular controller for the click is:
angular.module("umbraco").controller("My.MarkdownEditorController",
function ($scope, $http) {
$scope.changeValue = function () {
//changes value but it doesn't save
//what is the Angular way to do this correctly
document.getElementById('title').value= document.getElementById('title').value + '2';
};
});
How do I change the value of the adjacent text box with id "title" correctly?
The above code modifies the value, but it doesn't save.
I think that's because I am not changing it the Angular way.
The html of the input I am trying to modify is:
<input type="text" id="title" name="textbox" ng-model="model.value" class="umb-property-editor umb-textstring textstring ng-pristine ng-untouched ng-valid ng-isolate-scope ng-valid-val-server ng-not-empty ng-valid-required" val-server="value" ng-required="model.validation.mandatory" aria-required="false" aria-invalid="False" ng-trim="false" ng-keyup="change()">
I got this working but not sure if it is correct:
angular.module("umbraco").controller("My.MarkdownEditorController",
function ($scope, $http) {
$scope.changeValue = function () {
var title = $("#title");
title.val("this is how to do it");
setTimeout(function(){ title.trigger("input") }, 10);
};
});
Your input: <alue" ng-keyup="change()">
You have javascript value model.value and html input value. These two are connected via ng-model:
After you change scope value $scope.model.value = ... angularjs will update html input value.
After user changes html input value, angularjs will update your scope value.
You should nealy never change html input value directly, you should change scope value (it wont trigger ng-keyup or ng-change).
P.S. avoid using ng-keyup in inputs - there is copy-paste and drag-drop that may change input as well.
I have an html template attached to a controller and three directives. There are three buttons. On clicking a button a directive is added to the page(using ng-click) like this(the following is in my controller not in directive):
$scope.addFilterDimension = function() {
console.log('CLICKED!!!!!!!')
var divElement = angular.element(document.querySelector('#filterDimension'));
var appendHtml = $compile('<filter-directive></filter-directive>')($scope);
divElement.append(appendHtml);
}
Similarly for other buttons, other directives are added. Now, I can keep adding as many of these directives as I like, which is the use case here.
These directives are basically like forms containing either dropdowns, input boxes or both. The values user selects from the dropdowns or enters in input boxes have to be sent back to the backend to be stored in the DB.
This is one of the directives(others are very similar):
anomalyApp.directive('filterDirective', function() {
return {
restrict: "E",
scope: {},
templateUrl:'filter-dimension.html',
controller: function($rootScope, $scope, $element) {
$scope.dimensionsOld = $rootScope.dimensionsOld;
$scope.dimensions = $rootScope.dimensions;
$scope.selectedDimensionName = $rootScope.selectedDimensionName;
$scope.selectedDimensionValue = $rootScope.selectedDimensionValue;
$scope.extend = $rootScope.extend;
$scope.selectedExtend = $rootScope.selectedExtend;
$scope.isDateField = $rootScope.isDateField;
console.log($scope.dimensions);
$scope.Delete = function(e) {
//remove element and also destoy the scope that element
$element.remove();
$scope.$destroy();
}
}
}
});
Now, in my controller I assign $rootscope to my values which have to be used in the directives and thus catch them in the directive. Example:
$rootScope.dimensions = temp.map(d=>d.dimName);
$rootScope.selectedDimensionName = '';
$rootScope.selectedDimensionValue = '';
And this is how I retrieve my values from added directives:
var retriveValue = function() {
var filtersData = [];
var constraintsData = [];
var variablesData = [];
var ChildHeads = [$scope.$$childHead];
var currentScope;
while (ChildHeads.length) {
currentScope = ChildHeads.shift();
while (currentScope) {
if (currentScope.dimensions !== undefined){
filtersData.push({
filterDimensionName: currentScope.selectedDimensionName,
filterDimensionValue: currentScope.selectedDimensionValue,
filterDimensionExtend: currentScope.selectedExtend,
filterDimensionIsDateFiled: currentScope.isDateField
});
}
if (currentScope.constraintDimensions !== undefined){
filtersData.push({
constraintDimensionName: currentScope.selectedConstraintName,
constraintDimensionValue: currentScope.selectedConstraintValue,
constraintDimensionExtend: currentScope.selectedConstraintExtend,
constraintDimensionVariable: currentScope.selectedConstraintVariable,
constraintDimensionOperator: currentScope.selectedOperator,
constraintDimensionVariableValue: currentScope.constraintVariableValue,
constraintDimensionIsDateField: currentScope.isDateFieldConstraint
});
}
if (currentScope.variableNames !== undefined){
console.log('currentScope.selectedVariableVariable',currentScope.selectedVariableVariable);
filtersData.push({
variableName: currentScope.selectedVariableVariable,
variableOperator: currentScope.selectedVariableOperator,
variableValue: currentScope.variableVariableValue,
variableExtend: currentScope.selectedVariableExtend
});
}
currentScope = currentScope.$$nextSibling;
}
}
return filtersData;
}
This is one of the directive's template:
<div >
<div>
<label>Dimension</label>
<select class = "custom-select custom-select-lg mb-6" ng-model="selectedDimensionName" ng-options="dimension for dimension in dimensions">
<!-- <option ng-repeat="table in tables track by $index">{{table}}</option> -->
</select>
</div>
<div>
<label>Date Field</label>
<input type="checkbox" ng-model="isDateField">
</div>
<div>
<label>Value</label>
<select multiple class = "custom-select custom-select-lg mb-6" ng-model="selectedDimensionValue" ng-options="val for val in ((dimensionsOld | filter:{'dimName':selectedDimensionName})[0].dimValues)"></select>
</span>
</div>
<div>
<label>Extend</label>
<select class = "custom-select custom-select-lg mb-6" ng-model="selectedExtend" ng-options="val for val in extend"></select>
</span>
</div>
<button type="button" class="btn btn-danger btn-lg" ng-click="Delete($event)">Delete</button>
This is in the main html to add the directive:
<div id="filterDimension"> </div>
I know this is not a good way, but please suggest a better one.
Secondly, a new change has to be made where inside one of the directives there will be a button, clicking on which 2 dropdowns(or simply one more directive) will be added and can be added as many times as needed(just like the other directive).
The issue here is this one is a directive inside another directive and I am facing unusual behavior like:
When I add the parent directive it is fine, I add the child directives its fine, but when I add a second parent and try to add its child, they get appended inside the first directive.
Even if I somehow manage to get out of the above point I do not know how to retrieve values from such directives.
PS: I am new in AngularJS or front-end for that matter, the retriveValue() and using rootscope I got from somewhere online.
Console screenshot I'm having a problem with my code; I'm getting values upon clicking the button, and the same values are also showing in the console. However, I can't see the same values in the input field. Can anyone help with this?
$scope.getFees = function (id) {
getClients.getFeesPoints(id).then(function(response) {
$scope.fees = response.data.fees;
console.log($scope.fees);
});
};
<input type="text" ng-model="fees" class="mdltb-inp fee-inp"
name="fees" placeholder="35$" >
Check link of image 30 is the value of response data
Remove value="{{User}}" from the code and verify $scope.fees = response.data; binds a text value and not an object.
In case if the response.data is an object, that won't bind to an input text field. Find out the appropriate key from response.data object and bind input with that key. Also if the value is not binded still try to call $scope.$apply() after assigning the value
I this you should try this way
var app = angular.module("myapp", []);
app.controller("myCtrl", ['$scope', function($scope) {
$scope.fees="new";
$scope.getFees = function (id) {
//getClients.getFeesPoints(id).then(function(response)
//{
$scope.fees = 50;
console.log($scope.fees);
//});
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
<div ng-controller="myCtrl">
<input type="text" class="form-control" id="inputValue" name="uservalue" ng-model="fees" ng-required="true" autofocus="true" ng-blur="checkIfValid()"/>
<button ng-click="getFees(true)">Demo</button>
<h1>{{fees}}</h1>
</div>
</div>
I have Solved this by using $rootScope i.e. "$rootScope” is a parent object of all “$scope” angular objects created in a web page.
I tried to get the HTML page value to angularJS function , The below steps are which i tried.
HTML page :
<label class="item-input item-stacked-label">
<span class="input-label cont_det_label">First Name</span>
<p class="contact_display" id="txtFirstName" ng-model="testName">Satya</p>
</label>
angularJS Page :
.controller('SocialNetworkCtrl', ['$scope','$http','$state','ContactsService','$ionicNavBarDelegate','$ionicLoading','$ionicPopup',function($scope, $http, $state, ContactsService, $ionicNavBarDelegate, $ionicLoading,$ionicPopup) {
$scope.showUserProfile = function() {
$state.go("linkedin");
var firstname = (document.getElementById("txtFirstName").value);
}
}])
So I need var firstname = Satya ?? Is it correct way please guide me to access this value .
var firstName = $scope.testName
<input ng-model="testName" />
testName is the ng-model name that you have give. It will be automatically binded to your controller. No need the get the value using document.getElementById
Wrong usage , why ng-model in <p> tag??
Update
Change your fiddle with the following code, it will work. Also make sure framework is selected properly (as in the image)
<div ng-app ng-controller="testController">
<input ng-model="testDataName" ng-change="check()" /> {{testDataName}}
After ng-change : {{checkName}}
</div>
function testController($scope) {
$scope.testDataName="Dummy Name";
$scope.check = function () {
$scope.checkName=$scope.testDataName;
console.log($scope.checkName);
};
}
its a text node, you will require .innerHTML or '.innerText', .value is for form inputs
var firstname = (document.getElementById("txtFirstName").innerHTML);
and don't use ng-model on a p element, change it to like this
<p class="contact_display" id="txtFirstName">{{testName}}</p>
just use $scope.testName to get the value, no need for firstname = (document.getElementById("txtFirstName").innerHTML); querying DOM for value is jQuery style, use angular the $scope for 2 way bindings
Read more at official doc
Update here is updated function on loginCtrl
.controller('loginCtrl', ['$scope', function ($scope) {
$scope.testNameData = 'Satya';
$scope.doLogin = function() {
alert($scope.testNameData);
};
}])
If you really want to go jQuery way here is what you can do, its not recommended, you should use angular directive to do DOM manipulation
$scope.showUserPro = function() {
$ionicLoading.show();
// Here i need the value of <p tag>
var name = document.getElementById("txtFirstName"),
firstNameFromHtmlPtag = name.innerText;
console.log(firstNameFromHtmlPtag, 'Doing API Call 1');
}
I need live update my model when I selecting the checkboxes. But when I run this code : TypeError: Cannot read property 'map' of undefined.
After I remove the map function & added console.log(nv).
Then initially it print a empty array. Then while I selecting a checkbox it returns the object correctly. Here is my code. Looking for your help..
//my Controller
$scope.selection = [];
$scope.$watch('items|filter:{selected:true}', function (nv) {
$scope.selection = nv.map(function (item) {
return item.id;
});
}, true);
//view
<div ng-repeat="item in items | filter:global.query | orderBy: name">
<input class="tile-check" id="check-0" type="checkbox" name="delete"
ng-model="item.selected">
</div>
source : http://jsbin.com/ImAqUC/1
Solved. The problem was the $watch is called before set the item variable. So I put a if around that.
if(nv){
$scope.selection = nv.map(function (item) {
return item.id;
});
}