Call Controller function from the directive in angular JS - angularjs

I am using angular JS right now, in the i am using ui-bootstrap typeahead
I am trying scroll on demand logic in typeahead
i have tried this:
HTML:
<div class='container-fluid' ng-controller="TypeaheadCtrl">
<pre>Model: {{selected| json}}</pre>
<input type="text" ng-model="selected" maxlength="5" typeahead="country.name for country in countries | filter:$viewValue | limitTo:8">
</div>
JS:
angular.module('plunker', ['ui.bootstrap'])
.controller('TypeaheadCtrl', function ($scope) {
$scope.selected = undefined;
$scope.countries = [
{ name: "Afghanistan", code: "AF" },
{ name: "Aland Islands", code: "AX" },
{ name: "Albania", code: "AL" },
{ name: "Algeria", code: "DZ" },
{ name: "American Samoa", code: "AS" },
{ name: "Andorra", code: "AD" },
{ name: "Angola", code: "AO" },
{ name: "Anguilla", code: "AI" },
{ name: "Antarctica", code: "AQ" },
{ name: "Antigua and Barbuda", code: "AG" },
{ name: "Argentina", code: "AR" },
{ name: "Armenia", code: "AM" },
{ name: "Aruba", code: "AW" },
{ name: "Ascension Island", code: "AC" },
{ name: "Australia", code: "AU" },
{ name: "Austria", code: "AT" },
{ name: "Azerbaijan", code: "AZ" },
{ name: "Bahamas", code: "BS" },
{ name: "Bahrain", code: "BH" },
{ name: "Bangladesh", code: "BD" },
{ name: "Barbados", code: "BB" },
{ name: "Belarus", code: "BY" },
{ name: "Zimbabwe", code: "ZW" }
];
$scope.call= function(){
alert('reached end');
};
})
.directive('ul', function () {
return {
restrict: 'E',
link: function ($scope, element, attrs) {
element.bind('scroll', function (e) {
if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
// alert('end reached');
$scope.call();
}
})
}
}
});
But in the above try $scope.call(); function is not calling. Any one pls help me
REFERENCE PLUNKER
My actual requirement is when the scroll reaches the end, remaining records has to show in the typeahead

Make these changes to your directive, add a callBack scope variable in directive and add a callBack attribute in HTML , type function
.directive('ul', function () {
return {
restrict: 'E',
scope: {
callBack:"&"
}
link: function (scope, element, attrs) {
scope.callBack();
})
}
}
<div ul callBack="functionName"></div>

Related

Angular recursive directive to represent tree structure needs adding file list

I have been attempting to update this directive to add fileList into the tree structure, but I was not successful. The original works fine (https://embed.plnkr.co/plunk/JgQu3r), but it considers directories only. In my data structure any directory can have a fileList, as well as any number of nested directoryList and so on.
function MainController($scope) {
$scope.menu = [{
name: 'one',
directoryList: [{
name: 'one'
}, {
name: 'two'
}],
fileList: ['one.pdf', 'two.pdf', 'three.pdf']
},
{
name: 'two',
directoryList: [{
name: 'one',
directoryList: [{
name: 'one'
}],
fileList: ['one.pdf', 'two.pdf', 'three.pdf', 'four.pdf']
}]
},
{
name: 'three'
}
];
}
function myMenu() {
return {
scope: {
myMenu: '=myMenu'
},
template: '<li ng-repeat="item in myMenu"><my-menu-item></my-menu-item></li>',
link: function(scope, elem) {}
}
}
function myMenuItem($compile) {
return {
template: '<a href ng-bind="item.name" ng-click="show($event)"></a>',
link: function(scope, element) {
if (angular.isArray(scope.item.menu)) {
element.append($compile('<ul ng-if="collapsed" my-menu="item.menu"></ul>')(scope));
}
scope.show = function($event) {
scope.collapsed = !scope.collapsed;
}
}
}
}
angular.module('app', [])
.controller('MainController', MainController)
.directive('myMenu', myMenu)
.directive('myMenuItem', myMenuItem);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.14/angular.js"></script>
<body ng-app="app" ng-controller="MainController">
<ul my-menu="menu">
</ul>
</body>
You need to distinguish between files and folders. Just treat the folders like you treat it in your example, and treat your files separately. The following snippet works, but I recommend that you take a minute to think about why and how it works.
function MainController($scope) {
$scope.menu = [{
name: 'one',
directoryList: [{
name: 'one'
}, {
name: 'two'
}],
fileList: ['one.pdf', 'two.pdf', 'three.pdf']
},
{
name: 'two',
directoryList: [{
name: 'one',
directoryList: [{
name: 'one'
}],
fileList: ['one.pdf', 'two.pdf', 'three.pdf', 'four.pdf']
}]
},
{
name: 'three'
}
];
}
function myMenu() {
return {
scope: {
myMenu: '=myMenu'
},
template: '<li ng-repeat="item in myMenu"><my-menu-item></my-menu-item></li>',
link: function(scope, elem) {}
}
}
function myMenuItem($compile) {
return {
template: `
<a href ng-bind="item.name" ng-click="show($event)"></a>
`,
link: function(scope, element) {
if (angular.isArray(scope.item.directoryList)) {
element.append($compile('<ul ng-if="!collapsed" my-menu="item.directoryList"></ul>')(scope));
}
if (angular.isArray(scope.item.fileList)) {
element.append($compile(`
<ul ng-if="item.fileList && !collapsed">
<li ng-repeat="file in item.fileList">{{file}}</li>
</ul>
`)(scope));
}
scope.collapsed = true;
scope.show = function($event) {
scope.collapsed = !scope.collapsed;
}
}
}
}
angular.module('app', [])
.controller('MainController', MainController)
.directive('myMenu', myMenu)
.directive('myMenuItem', myMenuItem);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.14/angular.js"></script>
<body ng-app="app" ng-controller="MainController">
<ul my-menu="menu">
</ul>
</body>

angular dropdown multiselect to connect data between three dropdowns

I have three multiselect dropdowns:-
<label>Dropdown One</label>
<div ng-model="a.dp1" ng-dropdown-multiselect="" options="multiSelectArray" selected-model="dropDownOne" extra-settings="multiSelectSettings">
</div>
<label>Dropdown Two</label>
<div ng-model="a.dp2" ng-dropdown-multiselect="" options="multiSelectArray" selected-model="dropDownTwo" extra-settings="multiSelectSettings">
</div>
<label>Dropdown Three</label>
<div ng-model="a.dp3" ng-dropdown-multiselect="" options="multiSelectArray" selected-model="dropDownThree" extra-settings="multiSelectSettings">
</div>
Directive Code:-
(function () {
'use strict';
angular.module('myApp.components')
.directive('page', page);
page.$inject = ['$http', '$timeout', 'ApiServices'];
function page($http, $timeout, ApiServices) {
return {
restrict: 'EA',
scope: {},
controller: function ($scope) {
$scope.a = { };
$scope.dropDownOne = [];
$scope.dropDownTwo = [];
$scope.dropDownThree = [];
$scope.multiSelectArray = [{
name: "Ayan"
}, {
name: "Rita"
}, {
name: "Mohit"
}, {
name: "Shittal"
}, {
name: "Jayant"
}, {
name: "Sachin"
}, {
name: "Tina"
}, {
name: "Babita"
}, {
name: "Priya"
}];
$scope.multiSelectSettings = {
smartButtonMaxItems: 11,
scrollable: true,
displayProp: "name",
idProp: "name",
externalIdProp: "name"
};
},
templateUrl: 'js/folder/system/page.html'
};
}
})();
What I am trying to do here is when I select particular options from 'Dropdown One' the same options got selected in 'Dropdown Two' and 'Dropdown Three' and get disabled so that users can't unselect them. Also, the users can select more options in 'Dropdown Two' and 'Dropdown Three' if they want, but the options from 'Dropdown One' should be checked already and disabled.
I am trying to disable the options using 'disabled' attribute but not able to for selected options. Any idea how I can do that?

Angularjs directive scope seems not binding to value

Please spare me if this is a beginner type of question. I'm playing Highcharts.js with Angular.js, but it's seems like directive scope is not binding to value. Below are my codes.
HTML - but it's working inside this expressions {{xaxis}} {{seriesdata}}
<div class="container" ng-controller='chartController'>
<div class="row">
<div class="col-md-6">
<line-chart xaxis='xaxis' seriesdata='seriesdata'></line-chart>
{{xaxis}}
{{seriesdata}}
</div>
<div class="col-md-6">
<bar-chart options='pieOptions'></bar-chart>
</div>
</div>
</div>
directive.js
(function() {
'use strict';
angular
.module('chart')
.directive('lineChart', lineChartDirective);
/* #ngInject */
function lineChartDirective() {
return {
restrict: 'E',
replace: true,
template: '<div></div>',
scope: {
xaxis: '=xaxis',
seriesdata: '=seriesdata'
},
link: function(scope, element) {
Highcharts.chart(element[0], {
chart: {
type: 'spline',
zoomType: 'x'
},
plotOptions: {
line: {
dataLabels: {
enabled: true
}
}
},
title: {
text: 'Profit Ratio by Month'
},
xAxis: {
categories: scope.xaxis
},
yAxis: {
title: {
text: 'Profit %'
}
},
series: [
{
name: 'Total',
data: scope.seriesdata
}
]
});
}
}
}
})();
service.js
(function() {
'use strict';
angular
.module('chart')
.factory('chartData', chartDataFactory);
chartDataFactory.$inject = ['$http'];
/* #ngInject */
function chartDataFactory($http) {
function getData() {
return $http.get('../../data/sampleData.json').then(function(result) {
return result.data;
})
}
return {
getData: getData
}
}
})();
controller.js
(function() {
'use strict';
angular
.module('chart')
.controller('chartController', chartController);
chartController.$inject = ['$scope', 'chartData', 'lodash'];
/* #ngInject */
function chartController($scope, chartData, lodash) {
var vm = this;
chartData.getData().then(function(data) {
$scope.chartData = data;
$scope.xaxis = data.SEO[0]["SEO-MOD-001"]["SEO-END-001"];
$scope.seriesdata = data.SEO[0]["SEO-MOD-001"]["SEO-END-002"];
console.log($scope.xaxis);
console.log($scope.seriesdata);
});
$scope.pieOptions = {
chart: {
type: 'column'
},
title: {
text: 'Projects'
},
xAxis: {
categories: ['Noreil', 'Ken', 'Alfie', 'Francis']
},
yAxis: {
title: {
text: 'Completed Projects'
}
},
series: [
{
name: 'Quarter 1',
data: [23, 11, 34, 26]
},
{
name: 'Quarter 2',
data: [14, 20, 26, 30]
}
]
}
}
})();
When the directive link function gets executed, you don't have data yet. So try following in link function:
var unwatch = scope.$watch('[xaxis, seriesdata]', function (results) {
if (results[0] && results[1]) {
init();
unwatch();
}
}, true);
function init() {
// Highcharts.chart(element[0], { ...
}

Issues formly with Angular Material modal

I would like to show the forms in a modal using the Angle Material, but simply the controller is ignored.
Outside the modal, it operates normally, only the modal giving this problem ..
What did I doing wrong?
Any idea doque do?
angular.module('adminApp.forms', ['formly', 'formlyBootstrap', 'adminApp.services'], function config(formlyConfigProvider) {
formlyConfigProvider.setType({
name : 'repeatSection',
templateUrl : 'repeatSection.html',
controller : function($scope) {
$scope.formOptions = {formState: $scope.formState};
$scope.addNew = addNew;
$scope.copyFields = copyFields;
function copyFields(fields) {
return angular.copy(fields);
}
function addNew() {
$scope.model[$scope.options.key] = $scope.model[$scope.options.key] || [];
var repeatsection = $scope.model[$scope.options.key];
var lastSection = repeatsection[repeatsection.length - 1];
var newsection = {};
// if (lastSection) {
// newsection = angular.copy(lastSection);
// }
repeatsection.push(newsection);
}
}
});
})
.controller('FormsController', function($scope, $mdDialog){
$scope.addField = function(ev){
$mdDialog.show({
parent : angular.element(document.body),
controller : FormsBuilder,
ariaLabel : 'Adicionar Campo',
targetEvent : ev,
clickOutsideToClose : true,
templateUrl : 'partials/tpl/fields.html',
})
function FormsBuilder($scope, FIELDS) {
console.log('start!')
var vm = this;
// funcation assignment
vm.onSubmit = onSubmit;
init();
//vm.originalFields = angular.copy(vm.fields);
// function definition
function onSubmit() {
console.log('submit!');
return
$scope.field = new FIELDS(vm.model);
$scope.field.$save(function(){
$scope.showToast('Field Group ....Criado!');
});
}
function init() {
console.log('init!')
vm.model = {
fields: []
};
vm.fields = [
{
"key": "fieldGroup-name",
"type": "input",
"templateOptions": {
"placeholder": "Nome do Grupo",
"label": "One add-on on the left (icon)"
}
},
{
type: 'repeatSection',
key: 'fields',
templateOptions: {
btnText:'Adicionar novo campo',
fields: [
{
className: 'row',
fieldGroup: [
{
className: 'col-xs-4',
type: 'input',
key: 'fieldName',
templateOptions: {
label: 'Nome do campo:'
}
},
{
type: 'input',
key: 'fieldSlug',
className: 'col-xs-4',
templateOptions: {
label: 'Slug do campo:'
}
}
]
},
{
"type": "select",
"key": "Fieldtype",
"templateOptions": {
"label": "Field Type",
"required": true,
"options": [
{
"name": "Text Field",
"value": "input"
},
{
"name": "TextArea Field",
"value": "textarea"
},
{
"name": "Radio Buttons",
"value": "radio"
},
{
"name": "Checkbox",
"value": "checkbox"
}
]
}
},
{
type: 'checkbox',
model: 'formState',
key: 'selfExecuting',
templateOptions: {
label: 'Are you executing this trade?'
}
},
{
hideExpression: '!formState.selfExecuting',
fieldGroup: [
{
type: 'input',
key: 'relationshipName',
templateOptions: {
label: 'Name:'
}
},
{
type: 'select',
key: 'complianceApprover',
templateOptions: {
label: 'Compliance Approver:',
options: [
{
name: 'approver 1',
value:'some one 1'
},
{
name: 'approver 2',
value:'some one 2'
}
]
}
},
{
type: 'textarea',
key: 'requestorComment',
templateOptions: {
label: 'Requestor Comment',
rows: 4
}
}
]
}
]
}
}
];
}
}
};
});
Modal template:
<md-dialog aria-label="Adicionar Campo">
<md-dialog-content>
<h1>Nome do Grupo: {{vm.exampleTitle}}</h1>
<form ng-submit="vm.onSubmit()" novalidate>
<formly-form model="vm.model" fields="vm.fields" form="vm.form">
<md-button type="submit" class="md-raised md-primary">Salvar</md-button>
</formly-form>
</form>
</md-dialog-content>
</md-dialog>
<script type="text/ng-template" id="repeatSection.html">
<div>
<div class="{{hideRepeat}}">
<div class="repeatsection" ng-repeat="element in model[options.key]" ng-init="fields = copyFields(to.fields)">
<formly-form fields="fields" model="element" bind-name="'formly_ng_repeat' + index + $parent.$index">
</formly-form>
<div style="margin-bottom:20px;">
<md-button type="submit" class="md-raised md-primary" ng-click="model[options.key].splice($index, 1)">Remover campo</md-button>
</div>
<hr>
</div>
<md-button type="button" class="md-raised md-primary" ng-click="addNew()" >{{to.btnText}}</md-button>
</div>
</script>
RESOLVED:
$mdDialog.show({
parent : angular.element(document.body),
controller : FormsBuilder,
controllerAs : 'vm',
ariaLabel : 'Adicionar Campo',
targetEvent : ev,
clickOutsideToClose : true,
templateUrl : 'partials/tpl/fields.html',
})

How to wrap kendoAutoComplete in an AngularJS directive

I'm trying to wrap kendoAutocomplete in an Angular directive. The problem is that I get the following error message: TypeError: 'undefined' is not a function (evaluating 'e(data)')
Any ideas what could be the cause of the problem ?
<input id="selectDomain" select-kendo=""/>
-----
var kendo = angular.module('kendo',[]);
kendo.directive('selectKendo', function () {
var data = [
"Sweden",
"Switzerland",
"Turkey",
"Ukraine",
"United Kingdom",
"Vatican City"
];
return {
restrict: 'A',
scope: {
},
link: function (scope, element) {
console.log(element);
$(element).kendoAutoComplete({
dataSource: data,
filter: "startswith",
placeholder: "Domain",
separator: ", "
});
}
}
});

Resources