I've got some issues with ng-model and input element. Please take a look at this plunker: http://plnkr.co/edit/PJCv1AsPns1cxuSPTZiU
My point is when I edit the input value and add some white spaces at the beginning and at the end f.e.
some text
and click save the white spaces are trimmed (and it's OK) from inputValue, but if I edit it again the "previous" white spaces appear in the input. How to prevent it? I tried to do this with
angular.element($('#trimInput')).val($scope.inputValue);
and it works, but I don't like this solution.
angular.module('app', [])
.controller('trim', ['$scope', function($scope) {
$scope.inputValue = "some text";
$scope.editMode = false;
$scope.edit = function() {
$scope.editMode = true;
};
$scope.save = function() {
$scope.editMode = false;
//angular.element($('#trimInput')).val($scope.inputValue);
console.log($scope.inputValue);
console.log($scope.inputValue.length);
};
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
<div ng-controller="trim">
<div ng-show="!editMode">
[{{inputValue}}]
<button ng-click="edit()">edit</button>
</div>
<div ng-show="editMode">
<input id="trimInput" type="text" ng-model="inputValue" />
<button ng-click="save()">save</button>
</div>
</div>
</body>
By default ng-trim is true in angular so it will automatically trim value in scope. But value will not update on input, means extra input will be there. You have to manually assign it.
$document[0].getElementById("trimInput").value = $scope.inputValue;
For this you need to inject $document in your application. This is automatically assign trim value to input when you save.
See DEMO
Use
var temp=document.getElementById('trimInput');
console.log(temp.value);
$scope.inputValue=temp.value
and
Why is this Angular JS filter removing spaces?
Related
I am looking at text input on AngularJS as it changes by the user.
However, it seems that when I print to the console from my .js, the ng-model is off by one character because there seems to be a lag with the update by one keydown.
For example:
If the user types "hello" I see "hell" until another keydown is triggered, which will then update what I see to "hello" but the user input could be "helloW"
If that makes sense... Sorry.
Basically, is there a way that I can force update my ng-model from my controller so that I can see the user input as it comes in with each keydown?
I cannot post my code, sorry.
Use ng-change with ng-model
var myApp = angular.module('app',[]);
myApp.controller('Main', function ($scope) {
$scope.print = function(){
console.log($scope.myname);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="Main">
<input type="text" name="inputbox" ng-change="print()" ng-model="myname">
</body>
try using "ng-keyup" with "ng-model" instead of ng-keydown :)
Here the doc:
https://docs.angularjs.org/api/ng/directive/ngKeyup
var app = angular.module('app',[]);
app.controller('MyController', function ($scope) {
$scope.onKeyUpPrint = function(){
// You can name the function as you want
// scope will be triggered exactly on key Up
console.log($scope.inputModel);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="MyController">
<h1>Use ng-keyup<h1>
<input type="text" name="inputbox" ng-keyup="onKeyUpPrint()" ng-model="inputModel">
</body>
My requirement: Have to reset my select boxes on button click
Example :-
I have 3 select boxes. When I click on the select box some different data will come and after that the result will get published. I added a Remove button which resets only its parent select box. Now, what I want to know is,how to reset all the three select box on clicking the remove button.
sample code is as under :-
<button ng-click="removeObj(key,model1,0)">Remove</span></button>
controller code is as under :-
scope.removeObj = function(modelID, subModelID, selectBoxPos) {
modelID = 0;
subModelID = 0;
})
I want on click of removeObj function all modelID data get reset to zero.
Please help.
As I understand you do not need any parameters.. only create a meaningful name resetModels:
AngularJS Controller:
$scope.resetModels = function() {
// Set default value to your models...
$scope.modelID = 0;
$scope.subModelID = 0;
});
Html:
<button ng-click="resetScpeModels()">Remove</span></button>
If you want to reset all the values. You should use it like this
$scope.model = {};
$scope.model.modelID = 0;
$scope.model.subModelID = 0;
<input ng-model="model.modelID"/>
If you want to reset it. Call again
$scope.model = {};
Inside ng-click function.
When you give a name to your form it automatically gets added to the $scope.
In angular we are having a $setPristine() method on the $scope.formName. which should recursively take care of resetting your form.
You can reset a form by, $scope.formName.$setPristine(); but if you're binding a model object to your inputs, you need to take care of clearing those too, ie:
$scope.formModel={}; or angular.copy({}, formModel);
Working demo :
var app = angular.module('myApp', []);
app.controller('myController', function($scope) {
$scope.Reset = function(formModel) {
angular.copy({}, formModel);
$scope.formModel = {};
$scope.submitForm.$setPristine();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<form name="submitForm" ng-controller="myController">
<label for="first_field">First Model</label>
<input ng-model="formModel.firstModel" />
<br />
<label for="second_field">Second Model</label>
<input ng-model="formModel.secondModel" />
<br />
<button type="button" ng-click="Reset(formModel)">Reset</button>
</form>
</div>
I am creating one form using Bootstrap & AngularJS. I am using CK editor in my page as textarea. But I am not able to retrieve the value of the textarea while the value of the input text field is easily captured in my AngularJS controller. Following is the code snippet:
HTML page:
<div class="container">
<div ng-controller="controller">
<form role="form">
<label for="sd"><b>Short Description: </b></label>
<input ng-model="sdesc" class = "form-control input-xxlarge" type = "text" placeholder ="Provide a short description here."/>
<br/>
<label for="dt"><b>Details: </b></label>
<textarea ng-model="details" class="form-control" name="details_editor" id="details_editor"></textarea>
<br/>
<button class = "btn btn-primary" ng-click="submitted()">Ask It!</button>
<script>
CKEDITOR.replace('details_editor');
</script>
</form>
</div>
<br/>
<hr>
</div>
JS
app.controller('controller', ['$scope', function($scope){
$scope.submitted = function(){
var sdesc = $scope.sdesc;
var details = $scope.details;
alert($scope.details);
};
}]);
The alert shows undefined for the text area value.
Please help me solve the issue.
You are using the plain Javascript version of CK editor and hence Angular is not getting notified to update the ng-model value of that textarea.
Basically, Angular runs a digest cycle to update all views and models but since in this case the values being changed in the CK editor is happening outside the Angular.s context which is not updating the ng-model value.
To fix this, we added a small directive and notifying the change in the ng-model to the Angular by using the $timeout. (We can also use the $apply directive, but it may fail sometimes if the digest cycle is already in progress)
Example directive:
var app = angular.module("your-app-name", []);
app.directive("ckEditor", ["$timeout", function($timeout) {
return {
require: '?ngModel',
link: function ($scope, element, attr, ngModelCtrl) {
var editor = CKEDITOR.replace(element[0]);
console.log(element[0], editor);
editor.on("change", function() {
$timeout(function() {
ngModelCtrl.$setViewValue(editor.getData());
});
});
ngModelCtrl.$render = function (value) {
editor.setData(ngModelCtrl.$modelValue);
};
}
};
}]);
Remove, your following code:
<script>
CKEDITOR.replace('details_editor');
</script>
And, modify your text-editor like:
<textarea ng-model="details" class="form-control" ck-editor name="details_editor" id="details_editor"></textarea>
I found ng-ckeditor to implement ckeditor in angularjs.
Please refer this :https://github.com/esvit/ng-ckeditor. I tried it, It is easy to implement and working as expected
I want to prevent the user from deleting certain length of string from textbox in AngularJS.
I want some text like "abcd" to be fixed in textbox such that user cannot delete it. It would act like pre-string.
For that I have initialized the textbox with ng-init and on keyup event I'm checking the length of string, if it is less than predefined size then I'm putting the string back in the ng-model.
It's working with no errors, but not smoothly. I want to prevent pressing backspace or delete button if the text reached to specified length while deleting the text from textbox.
I am new to AngularJS.
Here is the Plunker link.
HTML
<body ng-app="myApp1">
<div ng-controller="myController">
<div ng-app>
<label>Key:</label>
<input type="text" name="key" ng-model="key" ng-init="key = 'abcd'" ng-keyup="updateKey($event)">
</div>
</div>
</body>
AngularJS
(function(angular) {
'use strict';
var myApp = angular.module('myApp1', []);
myApp.controller('myController', ['$scope', function($scope) {
$scope.updateKey = function($event) {
var len = $scope.key.length;
if ($event.keyCode === 8 && len < 4) {
$scope.key = "abcd";
}
};
}]);
})(window.angular);
I think you can setup a explicit $watcher function for the $scope.key
$scope.$watch('key', function(newValue, oldValue) {
console.log(oldValue, newValue);
if(newValue.length < 4) {
$scope.key = 'abcd';
}
});
watcher function will call every time when the model is change, so when you change the model angular will call this function, in the function we can get the old value and new value based on that you can do what you try to achive.
here is the DEMO
I'm fairly new using AngularJS but I've been using for a pet project and I've run in to an issue. What I basically want to do is take the text input from this input field:
<form id="ci_search_form">
<p class="input-append"><label>Search:</label> <input type="text" id="query" ng:model="query" autofocus> <button ng:click="clearSearch()" class="btn"><i class="icon-remove"></i></button></p>
</form>
and update this input field's value with that value:
<div><input type="text" id="ciquery" ng:model="ciquery.Name"></div>
The second input filters some data and I can type in that directly and it works. However this page will have different sets of data, each with their own search input that I want updated by the master input at the top. I can't set value="" or use jQuery to set the value either, it just appears blank unless I explicitly type in that second input field. Can anyone assist with a solution?
EDIT
I thought I should include my app and controller code:
var App = angular.module('TicketAssistApp', []);
App.controller('SearchController', function($scope, $http, $filter){
$scope.query = '';
$http.get('static/ci_list.json').success(function(data){
$scope.ci_list = data;
});
$scope.clearSearch = function(){
$scope.query = '';
}
});
EDIT 2
Made some progress. Created a function that can be called an update ciquery in $scope:
var App = angular.module('TicketAssistApp', []);
App.controller('SearchController', function($scope, $http, $filter){
$scope.query = '';
$scope.ciquery = '';
$http.get('static/ci_list.json').success(function(data){
$scope.ci_list = data;
});
$scope.queryUpdate = function(){
$scope.ciquery = $scope.query;
}
$scope.clearSearch = function(){
$scope.query = '';
$scope.queryUpdate();
}
});
This works great. However, this creates another issue. Before in ciquery I was using ciquery.Name to filter only on the Name attribute. With this new solution I had to change it to this:
<div><input type="hidden" id="ciquery" ng:model="ciquery"></div>
This searches all fields in my data which returns unwanted results. Suggestions?
$scope and ng-model are differents. You should give ng-model's property to ng-click's function. Looks at this -> Ng-model does not update controller value
To update second input's field (here an example -> http://jsfiddle.net/yEvSL/1/)
<div><input type="text" id="ciquery" ng:model="query"></div>