Updating model from directive in angularjs - angularjs

I have a directive to drag and drop.
The drag and drop works well, but I have a problem with updating the model.
After I drop some text into textarea, the text is showing ok, but the model is not updating.
What am I missing here?
//markup
<textarea drop-on-me id="editor-texto" ng-trim="false" ng-model="mymodel"
name="templateSms.template">test.</textarea>
//directive
angular
.module('clinang')
.directive('dragMe', dragMe)
.directive('dropOnMe', dropOnMe);
dragMe.$inject = [];
function typeInTextarea(el, newText) {
var start = el.selectionStart
var end = el.selectionEnd
var text = el.value
var before = text.substring(0, start)
var after = text.substring(end, text.length)
el.value = (before + newText + after)
el.selectionStart = el.selectionEnd = start + newText.length
el.focus()
}
function dragMe() {
var DDO = {
restrict: 'A',
link: function(scope, element, attrs) {
element.prop('draggable', true);
element.on('dragstart', function(event) {
event.dataTransfer.setData('text', event.target.id)
});
}
};
return DDO;
}
dropOnMe.$inject = [];
function dropOnMe() {
var DDO = {
restrict: 'A',
link: function(scope, element, attrs) {
element.on('dragover', function(event) {
event.preventDefault();
});
element.on('drop', function(event) {
event.preventDefault();
var data = event.dataTransfer.getData("text");
var x=document.getElementById(data);
typeInTextarea(event.target,x.getAttribute('data-value'))
});
}
};
return DDO;
}

Update your textarea model inside typeInTextarea function and using $apply run digest cycle to update the model change across whole app. For that with your current structure of directives with only link functions you'll need to pass scope to the typeInTextarea function (as a parameter).
So your function will be:
function typeInTextarea(scope, el, newText) {
var start = el.selectionStart
var end = el.selectionEnd
var text = el.value
var before = text.substring(0, start)
var after = text.substring(end, text.length)
el.value = (before + newText + after);
scope.mymodel.textnote = el.value;
el.selectionStart = el.selectionEnd = start + newText.length;
el.focus();
}
and dropOnMe function will be:
function dropOnMe() {
var DDO = {
restrict: 'A',
link: function(scope, element, attrs) {
element.on('dragover', function(event) {
event.preventDefault();
});
element.on('drop', function(event) {
event.preventDefault();
var data = event.dataTransfer.getData("text");
var x=document.getElementById(data);
typeInTextarea(scope, event.target,x.getAttribute('data-value'))
scope.$apply();
});
}
};
return DDO;
}
Check out this example (I don't know which element you're dragging so e.g. I've considered span element & just used innerHTML for that ):
https://plnkr.co/edit/wGCNOfOhoopeZEM2WMd1?p=preview

Related

Angularjs, Range.insertNode, ngClick : After $compile Angular isn't aware of element - ngClick not working

ngClick is still not responding even after $compile. The new element is being applied to the DOM and is accessible via jQuery and JS. I assume that the issue is with the range.insertNode function. What am I missing here?
Here's my directive:
.directive('selectText', [
'$rootScope',
'$compile',
'$window',
function ($rootScope, $compile, $window) {
return {
restrict: 'A',
scope: {
hlid: "=",
tu: "="
},
link: function (scope, element, attrs) {
element.on('mouseup', function () {
//console.log("Attrs: "+JSON.stringify(attrs));
if ($window.getSelection().toString()) {
var text = $window.getSelection().toString();
if(text == '') {
console.log("No selection");
return;
}
var selection = $window.getSelection();
var range = selection.getRangeAt(0);
var selectionContents = range.extractContents();
var clk = "edSel('hl_"+scope.hlid+"','"+attrs.id+"');";
// var span = $compile(angular.element('<hlight id="hl_'+scope.hlid+'" class="cr-pr noselect clickable" title="Text Selection" ng-click="'+clk+'">'+text+'</hlight>'))(scope);
var span = angular.element($compile('<hlight id="hl_'+scope.hlid+'" class="cr-pr noselect clickable" title="Text Selection" ng-click="'+clk+'">'+text+'</hlight>')(scope));
console.log(span);
range.insertNode(span[0]);
scope.tu.target = element.html();
//selection.removeAllRanges();
var arr = {};
arr.action = 'add';
arr.tuid = attrs.id;
arr.hlid = 'hl_'+scope.hlid;
arr.content = element.html();
scope.$emit('hlChange', arr);
scope.hlid++;
console.log(element.html());
var modal = UIkit.modal("#hl_modal");
modal.show();
}
});
scope.edSel = function(id,tuid) {
console.log('ID: '+id+" - tuID: "+tuid);
}
}
};
}])
Thanks for any help

AngularJS directive is not updating template if controller name is not attached

If I specify the controller property in the code below then my code will work. Otherwise the code is not updating the HTML template:
function ngVisualizer($http, $timeout, SharedFactory, ThreejsFactory) {
return {
restrict: 'A',
template: '<div id="canvas"></div>',
transclude: 'true',
priority: Number.MIN_SAFE_INTEGER, //Lowest priority
link: function (scope, elem, attr, ctrl) {
angular.element(document).ready(function () {
var log = [];
// play audio sound.
var audio = new Audio('/Audio/Om_Sound.mp3');
audio.loop = true;
//audio.play();
scope.$watch("fileData", function (newValue, oldValue) {
if (SharedFactory.getRefreshStatus()) {
scope.RemoveEntity();
scope.dateTime = 0;
debugger;
console.log(scope.fileData);
//ESTS service and service name created and added to scene.
var planetSize = scope.centralPlanetSize(scope.countOfMachines);
var centralPlanet = scope.drawCentralPlanet(planetSize, scope.getColor(scope.planetPressure, 100));
var centralPlanetText = scope.draw2DText(scope.sliceName, scope.destinationPoint, planetSize);
scope.sceneObject.add(centralPlanet);
scope.sceneObject.add(centralPlanetText);
//scope.nwScene.add(centralPlanet);
//scope.nwScene.add(centralPlanetText);
//scope.central = null;
angular.forEach(scope.fileData, function (value, key) {
try {
var sphere = new THREE.Mesh(scope.outerGeometry, scope.outerMaterial);
//line creation among services
var sourcePoint = new THREE.Vector3(scope.destinationPoint.x, scope.destinationPoint.y, scope.destinationPoint.z);
var destinationPoint = new THREE.Vector3(ThreejsFactory.getPlanetList()[key].x, ThreejsFactory.getPlanetList()[key].y, ThreejsFactory.getPlanetList()[key].z);
//var path = new THREE.LineCurve3(sourcePoint, destinationPoint);
var path = scope.getCurvePath(sourcePoint, destinationPoint);
//var hexColorValue = scope.getHexColorValue(value.MaxCount);
var tube = scope.drawPipe(path, scope.getColor(value.MaxCount, 65000));
//text for service name
var serviceTextGeometry = scope.draw2DText(value.ServiceName , ThreejsFactory.getPlanetList()[key], 40);
//scope.nwScene.add(serviceTextGeometry);
scope.sceneObject.add(serviceTextGeometry);
//sphere for service name
sphere.position.set(ThreejsFactory.getPlanetList()[key].x, ThreejsFactory.getPlanetList()[key].y, ThreejsFactory.getPlanetList()[key].z);
//scope.nwScene.add(tube);
scope.sceneObject.add(tube);
//scope.nwScene.add(sphere);
scope.sceneObject.add(sphere);
//from ests
var toChild = scope.drawTorusIn(tube.geometry.parameters.path.v2, scope.pipeWidth, scope.getDonutFatness(value.DataBytesIn), "red");
//scope.nwScene.add(toChild);
scope.sceneObject.add(toChild);
//to ests
var toParent = scope.drawTorusOut(tube.geometry.parameters.path.v1, scope.pipeWidth, scope.getDonutFatness(value.DataBytesOut), "blue");
//scope.nwScene.add(toParent);
scope.sceneObject.add(toParent);
scope.torusMovementPositive(tube, toParent, value.AvgSRTT);
scope.torusMovementNegative(tube, toChild, value.AvgSRTT);
//start
scope.addToServiceList(value, tube, sphere, serviceTextGeometry, toChild, toParent);
} catch (err) {
console.log(err);
}
}, log);
scope.nwScene.add(scope.sceneObject);
}
});
});
}
//,
//controller: "LandingPageController"
}
};
I am not able to figure out the root cause behind this issue. If the controller property is uncommented then it starts working, otherwise not.

Uneditable trailing text in textarea

I have been fighting with this for a couple of days to no avail.
I need to have a textarea with uneditable text trailing on the end.
Here is a fiddle with the text in the beginning of the text area, the requirement is to have it on the end. Anyone have any insight how to achieve this?
http://jsfiddle.net/codemonkeytony/3ew5h6bf/7/
var partialApp = angular.module("partialApp", []);
partialApp.directive('partialReadonly', function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
elem.on('keypress, keydown', function (event) {
console.log(scope.bbb);
console.log(event);
var readOnlyLength = attrs["partialReadonly"].length;
if ((event.which != 37 && (event.which != 39)) && ((elem[0].selectionStart < (scope.bbb - readOnlyLength )) || ((elem[0].selectionStart != (scope.bbb - readOnlyLength )) && (event.which == 8)))) {
event.preventDefault();
}
});
$(window).load(function () {
elem[0].value = attrs["partialReadonly"];
});
}
};
});
I have updated the logic on keypress event handler. Check this updated fiddle, if you want this behaviour.
partialApp.directive('partialReadonly', function () {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var readOnlyText = attrs["partialReadonly"];
var readOnlyLength = readOnlyText.length;
var ele = elem[0];
elem.on('keypress, keydown', function (event) {
var actualTextLength = ele.value.length - readOnlyLength;
var readOnlyTextStartIndex = actualTextLength;
if (elem[0].selectionStart > readOnlyTextStartIndex) {
event.preventDefault();
}
});
$(window).load(function () {
elem[0].value = attrs["partialReadonly"];
});
}
};
});

AngularJS - bind to directive resize

How can i be notified when a directive is resized?
i have tried
element[0].onresize = function() {
console.log(element[0].offsetWidth + " " + element[0].offsetHeight);
}
but its not calling the function
(function() {
'use strict';
// Define the directive on the module.
// Inject the dependencies.
// Point to the directive definition function.
angular.module('app').directive('nvLayout', ['$window', '$compile', layoutDirective]);
function layoutDirective($window, $compile) {
// Usage:
//
// Creates:
//
var directive = {
link: link,
restrict: 'EA',
scope: {
layoutEntries: "=",
selected: "&onSelected"
},
template: "<div></div>",
controller: controller
};
return directive;
function link(scope, element, attrs) {
var elementCol = [];
var onSelectedHandler = scope.selected();
element.on("resize", function () {
console.log("resized.");
});
$(window).on("resize",scope.sizeNotifier);
scope.$on("$destroy", function () {
$(window).off("resize", $scope.sizeNotifier);
});
scope.sizeNotifier = function() {
alert("windows is being resized...");
};
scope.onselected = function(id) {
onSelectedHandler(id);
};
scope.$watch(function () {
return scope.layoutEntries.length;
},
function (value) {
//layout was changed
activateLayout(scope.layoutEntries);
});
function activateLayout(layoutEntries) {
for (var i = 0; i < layoutEntries.length; i++) {
if (elementCol[layoutEntries[i].id]) {
continue;
}
var div = "<nv-single-layout-entry id=slot" + layoutEntries[i].id + " on-selected='onselected' style=\"position:absolute;";
div = div + "top:" + layoutEntries[i].position.top + "%;";
div = div + "left:" + layoutEntries[i].position.left + "%;";
div = div + "height:" + layoutEntries[i].size.height + "%;";
div = div + "width:" + layoutEntries[i].size.width + "%;";
div = div + "\"></nv-single-layout-entry>";
var el = $compile(div)(scope);
element.append(el);
elementCol[layoutEntries[i].id] = 1;
}
};
}
function controller($scope, $element) {
}
}
})();
Use scope.$watch with a custom watch function:
scope.$watch(
function () {
return [element[0].offsetWidth, element[0].offsetHeight].join('x');
},
function (value) {
console.log('directive got resized:', value.split('x'));
}
)
You would typically want to watch the element's offsetWidth and offsetHeight properties. With more recent versions of AngularJS, you can use $scope.$watchGroup in your link function:
app.directive('myDirective', [function() {
function link($scope, element) {
var container = element[0];
$scope.$watchGroup([
function() { return container.offsetWidth; },
function() { return container.offsetHeight; }
], function(values) {
// Handle resize event ...
});
}
// Return directive definition ...
}]);
However, you may find that updates are quite slow when watching the element properties directly in this manner.
To make your directive more responsive, you could moderate the refresh rate by using $interval. Here's an example of a reusable service for watching element sizes at a configurable millisecond rate:
app.factory('sizeWatcher', ['$interval', function($interval) {
return function (element, rate) {
var self = this;
(self.update = function() { self.dimensions = [element.offsetWidth, element.offsetHeight]; })();
self.monitor = $interval(self.update, rate);
self.group = [function() { return self.dimensions[0]; }, function() { return self.dimensions[1]; }];
self.cancel = function() { $interval.cancel(self.monitor); };
};
}]);
A directive using such a service would look something like this:
app.directive('myDirective', ['sizeWatcher', function(sizeWatcher) {
function link($scope, element) {
var container = element[0],
watcher = new sizeWatcher(container, 200);
$scope.$watchGroup(watcher.group, function(values) {
// Handle resize event ...
});
$scope.$on('$destroy', watcher.cancel);
}
// Return directive definition ...
}]);
Note the call to watcher.cancel() in the $scope.$destroy event handler; this ensures that the $interval instance is destroyed when no longer required.
A JSFiddle example can be found here.
Here a sample code of what you need to do:
APP.directive('nvLayout', function ($window) {
return {
template: "<div></div>",
restrict: 'EA',
link: function postLink(scope, element, attrs) {
scope.onResizeFunction = function() {
scope.windowHeight = $window.innerHeight;
scope.windowWidth = $window.innerWidth;
console.log(scope.windowHeight+"-"+scope.windowWidth)
};
// Call to the function when the page is first loaded
scope.onResizeFunction();
angular.element($window).bind('resize', function() {
scope.onResizeFunction();
scope.$apply();
});
}
};
});
The only way you would be able to detect size/position changes on an element using $watch is if you constantly updated your scope using something like $interval or $timeout. While possible, it can become an expensive operation, and really slow your app down.
One way you could detect a change on an element is by calling
requestAnimationFrame.
var previousPosition = element[0].getBoundingClientRect();
onFrame();
function onFrame() {
var currentPosition = element[0].getBoundingClientRect();
if (!angular.equals(previousPosition, currentPosition)) {
resiszeNotifier();
}
previousPosition = currentPosition;
requestAnimationFrame(onFrame);
}
function resiszeNotifier() {
// Notify...
}
Here's a Plunk demonstrating this. As long as you're moving the box around, it will stay red.
http://plnkr.co/edit/qiMJaeipE9DgFsYd0sfr?p=preview
A slight variation on Eliel's answer worked for me. In the directive.js:
$scope.onResizeFunction = function() {
};
// Call to the function when the page is first loaded
$scope.onResizeFunction();
angular.element($(window)).bind('resize', function() {
$scope.onResizeFunction();
$scope.$apply();
});
I call
$(window).resize();
from within my app.js. The directive's d3 chart now resizes to fill the container.
Here is my take on this directive (using Webpack as bundler):
module.exports = (ngModule) ->
ngModule.directive 'onResize', ['Callback', (Callback) ->
restrict: 'A'
scope:
onResize: '#'
onResizeDebounce: '#'
link: (scope, element) ->
container = element[0]
eventName = scope.onResize || 'onResize'
delay = scope.onResizeDebounce || 1000
scope.$watchGroup [
-> container.offsetWidth ,
-> container.offsetHeight
], _.debounce (values) ->
Callback.event(eventName, values)
, delay
]

angularjs + typeahead input field initial vaule

I have this directive, which is basicly wrapper for typeahead plugin from bootstrap. Everything is working like a charm. But now I have to set initial vaule in typeahead's input field. The value is passed as a string in attrs.init. But I don't know how to insert it into text field.
angular.module('rcApp')
.directive('rcAutocomplete', ['$injector', function ($injector) {
return {
scope: {
model: '#',
search: '#',
key: '#',
show: '#',
init: '#',
ngModel: '='
},
template: '<input type="text">',
replace: true,
restrict: 'E',
require: 'ngModel',
link: function (scope, element, attrs) {
// inject model service
var service = $injector.get(attrs.model);
// define search function
var searchFunction = attrs.search;
// holds picked object id
scope.id = 0;
// holds objects that matched query, mapped by "show" value
scope.map = {};
element.on('focusout ac.itempicked', function () {
scope.$apply(function () {
scope.ngModel = scope.id;
});
});
// launch typehead plugin
element.typeahead(
{
source: function (query, process) {
// clear cache
scope.id = 0;
scope.map = {};
service[searchFunction](query).then(function (result) {
var dataValues = [];
var fieldsToShow = scope.show.split('|');
$.each(result.data, function (index, dataItem) {
// generate key-show string
var valueHash = '';
for (var i = 0; i < fieldsToShow.length; i++) {
valueHash += dataItem[fieldsToShow[i]] + ' ';
}
valueHash = $.trim(valueHash);
// map results
scope.map[valueHash] = dataItem;
// prepare return strings
dataValues.push(valueHash);
});
// return content
process(dataValues);
});
},
updater: function (item) {
if (typeof scope.key === 'undefined') {
scope.id = scope.map[item];
}
else {
scope.id = scope.map[item][scope.key];
}
element.trigger('ac.itempicked');
return item;
}
}
);
}
};
}]);
** UPDATE **
Solution, that worked for me is adding code like this to link function:
// init value
if (typeof attrs.init !== 'undefined') {
window.setTimeout(function () {
element.val(attrs.init);
scope.$apply();
}, 10);
}
But still I don't quite understand why "element.val(attrs.init);" don't updated the view, and calling scope.$apply() did, but throw an "$digest already in progress" error. Wrapping it in window.setTimeout helped, but this also is a hack for me....
It's got to be a way to make it cleaner/simpler...

Resources