Passing value from Jquery event to AngularJS - angularjs

I have a ASP.NET MVC web application.I am replacing a existing table control only (JQuery html coded) with AngularJS grid.
I want to pass id value to AngularJs app from jQuery click event. On click event below code is not working.Please suggest the right way to pass the value.Using angular.element is not working.
$(".bs-example").on('click', '#alldiv a', function () {
var id = $(this).attr("id");
// This id is required to use in angular app
angular.element($("#my-table")).scope().getData();
});
var myApp = angular.module('myGrid', ['ngTable']);
myApp.controller('gridCtrl', ['$scope', '$http', 'services', 'NgTableParams', function (scope, http, ser, NgTableParams) {
ser.getData().success(function (response) {
scope.agendas = response;
scope.tableParams = new NgTableParams({ page: 1, count: 2 }, { dataset: scope.mydata });
});
scope.mydata = {
data: 'mydata'
}
}]);
myApp.service('services', function ($http) {
this.getData = function () {
// Need id from button jQuery click event
var result = $http.get('/Home/GetData/' + id);
return result;
};
});
<div data-ng-app="myGrid" data-ng-controller="gridCtrl" id="my-table">
//.....
</div

This should work.
EDIT:
$(".bs-example").on('click', '#alldiv a', function () {
var id = $(this).attr("id");
//HEre you pass a id to angular controller.
angular.element($("#my-table")).scope().getData(id);
});
var myApp = angular.module('myGrid', ['ngTable']);
myApp.controller('gridCtrl', ['$scope', '$http', 'services', 'NgTableParams', function (scope, http, ser, NgTableParams) {
$scope.GetData = function(id) {
ser.getData(id).success(function (response) {
scope.agendas = response;
scope.tableParams = new NgTableParams({ page: 1, count: 2 }, { dataset: scope.mydata });
});
}
scope.mydata = {
data: 'mydata'
}
}]);
myApp.service('services', function ($http) {
//Add to this function parameter
this.getData = function (id) { //here declare that this function
// Need id from button jQuery click event
var result = $http.get('/Home/GetData/' + id);
return result;
};
});
IHMO better option here will be use a ng-click angular method on the div that you wana click.
https://docs.angularjs.org/api/ng/directive/ngClick

I am able to call Angular function from JQuery with following code
$(".bs-example").on('click', '#alldiv a', function () {
var id = $(this).attr("id");
var scope = angular.element(document.getElementById("my-table")).scope();
scope.$apply(function () {
scope.save(id);
});
});

Related

Pass Service As A Controller Parameter

I've a AngularJs controller that gets a list of categories from ASP.NET MVC controller. It works just fine and here is the below code:
productApp.controller('categoryController', function ($scope, categoryService) { //The controller here
$scope.Categories = null;
categoryService.GetCategories().then(function (d) {
$scope.Categories = d.data;
}, function () {
alert('Failed');
});
});
productApp.factory('categoryService', function ($http) { //The product service
var factory = {};
factory.GetCategories = function () {
return $http.get('/Product/GetCategories'); //The ASP.NET MVC controlled defined
}
return factory;
});
I am trying to convert it to the following passing the service as a controller parameter but it doesn't work:
(function () {
'use strict';
angular
.module('productApp', ['ngMessages'])
.controller('categoryController', categoryController);
categoryController.$inject = ['$scope', 'Products'];
function categoryController($scope, categoryService) {
$scope.Categories = null;
categoryService.GetCategories().then(function (d) {
$scope.Categories = d.data;
}, function () {
alert('Failed');
});
}
productApp.factory('categoryService', function ($http) { //The product service
var factory = {};
factory.GetCategories = function () {
return $http.get('/Product/GetCategories'); //The ASP.NET MVC controlled defined
}
return factory;
});
})();
My requirement is to get all the controller united as follows:
angular
.module('productApp')
.controller('categoryController', categoryController)
.controller('productController', productController);
I believe, I am close enough and missing something here. I would expect some suggestions to implement it in the correct way. Thanks.
Update 1 - The below code works almost but it doesn't bind the DropDownList with category data: But using alert method, it returns [Object][Object] that means it gets categories from database
(function () {
'use strict';
angular
.module('productApp', [])
.controller('categoryController', categoryController)
.factory('categoryService', categoryService);
categoryController.$inject = ['$scope', 'categoryService'];
function categoryController($scope, categoryService) {
$scope.Categories = null;
categoryService.GetCategories().then(function (d) {
$scope.Categories = "Service: " + d.data + ", Controller: categoryController";
alert(d.data); //Here it alert [Object][Object]
}, function () {
alert('Failed');
});
}
function categoryService($http) {
var factory = {};
factory.GetCategories = function () {
return $http.get('/Product/GetCategories');
}
return factory;
};
})();
In the view:
<div ng-app="productApp" ng-controller="categoryController">
<select ng-model="saveProducts.ParentId">
<option value="">----Please Select Category----</option>
<option ng-repeat="m in Categories" value="{{ m.CategoryId }}">{{ m.Category }}</option>
</select>
</div>
#user8512043 Here is a JSFiddle with the working code: http://jsfiddle.net/luisperezphd/2fvwz6wp/
(function () {
'use strict';
angular
.module('productApp', [])
.controller('categoryController', categoryController)
.factory('categoryService', categoryService);
categoryController.$inject = ['$scope', 'categoryService'];
function categoryController($scope, categoryService) {
$scope.Categories = null;
categoryService.GetCategories().then(function (d) {
$scope.Categories = d.data;
}, function () {
alert('Failed');
});
}
function categoryService($http, $q) {
var factory = {};
factory.GetCategories = function () {
//return $http.get('/Product/GetCategories');
return $q.when({ data: [
{ CategoryId: 1, Category: "Category 1" },
{ CategoryId: 10, Category: "Category 2" },
{ CategoryId: 20, Category: "Category 3" }
]});
}
return factory;
};
})();
Note that it looks like code was fine. The drop down wasn't working because you assigned a string to the scope variable instead of an array.
One technique to resolve issue like that is to make it as visual as you can. For example use an ng-repeat on a instead of an .
Also in general if you are going to ask for help on StackOverflow you are going to get a lot more responses if you create a JSFiddle or Plnkr with your sample code.
Finally if you are going to do that mock the server calls. This will allow the person who wants to answer to do some quick experiments.
Check this code
(function () {
'use strict';
angular
.module('productApp',[])
.controller('categoryController', categoryController)
.controller('productController', productController)
.factory('categoryService', categoryService);
function categoryController($scope, categoryService) {
$scope.data = "Service: "+categoryService.serviceName+", Controller: categoryController";
};
function productController($scope, categoryService) {
$scope.data = "Service: "+categoryService.serviceName+", Controller: productController";
};
function categoryService() {
return {serviceName: "categoryService"};
};})();

How to call a method from a controller to another controller in angular js

I have a view for SidebarController like below -
<a ng-click="reachMe($event);$event.preventDefault()" ng-href="#/app/hello">
Before going to the link I want to call reachMe() to check some changes on page and need to show an alert if any changes made
function SidebarController($rootScope, $scope, $state, $location, SidebarLoader){
$scope.reachMe = function(event){
//here I want to call function isPageChanged() from StaticPageController
//something like this
// if StaticPageController.isPageChanged() return true
// then show alert
// else
// $location.url($href)
}
}
Update 1 :
Not sure about this, But give it a try.
<div ng-app="testApp" ng-controller="ControllerOne">
<button ng-click="methodA();"> Call Another Controller</button>
</div>
<script>
var app = angular.module('testApp', []);
app.controller('ControllerOne', function($scope, $rootScope) {
$scope.reachMe = function() {
var arrayData = [1,2,3];
$rootScope.$emit('callEvent', arrayData);
if($rootScope.isChanged){
// Show Alert
}else{
//Go to route
}
}
});
app.controller('ControllerTwo', function($scope, $rootScope,$state) {
$scope.checkSomethingChanged = function() {
alert("Hello");
$rootScope.isChanged = true;
}
$rootScope.$on('callEvent', function(event, data) {
console.log(data);
$scope.checkSomethingChanged();
});
});
Following method worked for me perfectly :
<div ng-app="testApp" ng-controller="ControllerOne">
<button ng-click="methodA();"> Call Another Controller</button>
</div>
<script>
var app = angular.module('testApp', []);
app.controller('ControllerOne', function($scope, $rootScope) {
$scope.methodA = function() {
var arrayData = [1,2,3];
$rootScope.$emit('callEvent', arrayData);
}
});
app.controller('ControllerTwo', function($scope, $rootScope) {
$scope.reachMe = function() {
alert("Hello");
}
$rootScope.$on('callEvent', function(event, data) {
console.log(data);
$scope.reachMe();
});
});
</script>
A controller is not the right concept for sharing functionality. Use a Factory or Service for that.
var logicFactory = function () {
return {
methodA: function () {
},
methodB: function()
{
}
};
}
You can then inject that factory into each controller where it is needed like:
var ControllerA = function ($scope,logicFactory) {
$scope.logic = logicFactory;
}
ControllerA.$inject = ['$scope', 'logicFactory'];
Another option is to use the broadcast/emit Patern. But I would use that only where really necessary:
Usage of $broadcast(), $emit() And $on() in AngularJS

AngularJS Factory object not being updated on controller and view

I'm with a problem with binding an object of a Factory and a Controller and it's view.
I am trying to get the fileUri of a picture selected by the user. So far so good. The problem is that I am saving the value that file to overlays.dataUrl. But I am referencing it on the view and it isn't updated. (I checked and the value is actually saved to the overlays.dataUrl variable.
Here goes the source code of settings.service.js:
(function () {
"use strict";
angular
.module("cameraApp.core")
.factory("settingsService", settingsService);
settingsService.$inject = ["$rootScope", "$cordovaFileTransfer", "$cordovaCamera"];
function settingsService($rootScope, $cordovaFileTransfer, $cordovaCamera) {
var overlays = {
dataUrl: "",
options: {
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
destinationType: Camera.DestinationType.FILE_URI
}
};
var errorMessages = [];
var service = {
overlays: overlays,
selectOverlayFile: selectOverlayFile,
errorMessages: errorMessages
};
return service;
function selectOverlayFile() {
$cordovaCamera.getPicture(overlays.options).then(successOverlay, errorOverlay);
}
//Callback functions
function successOverlay(imageUrl) {
//If user has successfully selected a file
var extension = "jpg";
var filename = getCurrentDateFileName();
$cordovaFileTransfer.download(imageUrl, cordova.file.dataDirectory + filename + '.' + extension, {}, true)
.then(function (fileEntry) {
overlays.dataUrl = fileEntry.nativeURL;
}, function (e) {
errorMessages.push(e);
});
}
function errorOverlay(message) {
//If user couldn't select a file
errorMessages.push(message);
//$rootScope.$apply();
}
}
})();
Now the controller:
(function () {
angular
.module("cameraApp.settings")
.controller("SettingsController", SettingsController);
SettingsController.$inject = ["settingsService"];
function SettingsController(settingsService) {
var vm = this;
vm.settings = settingsService;
activate();
//////////////////
function activate(){
// Nothing here yet
}
}
})();
Finnally on the view:
<h1>{{vm.settings.overlays.dataUrl}}</h1>
<button id="overlay" class="button"
ng-click="vm.settings.selectOverlayFile()">
Browse...
</button>
Whenever I change the value in the factory, it doesn't change in the view.
Thanks in advance!
Unfortunately Factories in angularjs are not meant to be used as two way bindings. Factories and Services are only singletons. They are only there to be used when called.
Ex Factory:
app.factory('itemFactory', ['$http', '$rootScope', function($http, $rootScope) {
var service = {};
service.item = null;
service.getItem = function(id) {
$http.get(baseUrl + "getitem/" + id)
.then(function successCallback(resp) {
service.item = resp.data.Data;
$rootScope.$broadcast("itemready");
}, function errorCallback(resp) {
console.log(resp)
});
};
return service;
}]);
I use the $broadcast so if I call getItem my controller knows to go get the fresh data.
Ex Directive:
angular.module("itemApp").directive("item", ['itemFactory', '$routeParams', '$location', '$rootScope', '$timeout', function (itemFactory, $routeParams, $location, $rootScope, $timeout) {
return {
restrict: 'E',
templateUrl: "components/item.html",
link: function (scope, elem, attr) {
scope.item = itemFactory.item;
scope.changeMade = function(){
itemFactory.getItem(1);
}
scope.$on("itemready", function () {
scope.item = itemFactory.item;
})
}
}
}]);
So as you can see in my code above anytime I need a fresh item I use $broadcast and $on to update my service and directive. I hope this makes sense, feel free to ask any questions.
As pointed by Ohjay44, the factory is not updated on the view. The way to do it is using a directive (also as Ohjay44 said). To use $broadcast, $emit and $on and keep the encapsulation I did what is recommended by John Papa's Angular Style Guide: created a factory (in my case a named it comms).
Here goes the newly created directive (overlay.directive.js):
(function () {
angular
.module('cameraApp.settings')
.directive('ptrptSettingsOverlaysInfo', settingsOverlaysInfo);
settingsOverlaysInfo.$inject = ["settingsService", "comms"];
function settingsOverlaysInfo(settingsService, comms) {
var directive = {
restrict: "EA",
templateUrl: "js/app/settings/overlays.directive.html",
link: linkFunc,
controller: "SettingsController",
controllerAs: "vm",
bindToController: true // because the scope is isolated
};
return directive;
function linkFunc(scope, element, attrs, vm) {
vm.overlays = settingsService.overlays;
comms.on("overlaysUpdate", function (event, overlays) {
vm.overlays = overlays;
});
}
}
})();
I created overlay.directive.html with:
<div class="item item-thumbnail-left">
<img ng-src="{{vm.overlays.dataUrl}}">
<h2>{{vm.overlays.dataUrl}}</h2>
</div>
And finally I put an $emit on the settingsService where the overlay is updated:
(function () {
"use strict";
angular
.module("cameraApp.core")
.factory("settingsService", settingsService);
settingsService.$inject = ["comms", "$cordovaFileTransfer", "$cordovaCamera"];
function settingsService(comms, $cordovaFileTransfer, $cordovaCamera) {
var overlays = {
dataUrl: "",
options: {
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
destinationType: Camera.DestinationType.FILE_URI
}
};
var errorMessages = [];
var service = {
overlays: overlays,
selectOverlayFile: selectOverlayFile,
errorMessages: errorMessages
};
return service;
function selectOverlayFile() {
$cordovaCamera.getPicture(overlays.options).then(successOverlay, errorOverlay);
}
//Callback functions
function successOverlay(imageUrl) {
//If user has successfully selected a file
var extension = "jpg";
var filename = getCurrentDateFileName();
$cordovaFileTransfer.download(imageUrl, cordova.file.dataDirectory + filename + '.' + extension, {}, true)
.then(function (fileEntry) {
overlays.dataUrl = fileEntry.nativeURL;
// New code!!!!
comms.emit("overlaysUpdated", overlays);
}, function (e) {
errorMessages.push(e);
});
}
function errorOverlay(message) {
//If user couldn't select a file
errorMessages.push(message);
//$rootScope.$apply();
}
}
})();
I used an $emit instead of a broadcast to prevent the bubbling as explained here: What's the correct way to communicate between controllers in AngularJS?
Hope this helps someone else too.
Cheers!

Avoid repetition in Angular controller

I'm an angular newbie and I'm writing an Ionic app.
I finished my app and am trying to refactor my controller avoiding code repetition.
I have this piece of code that manages my modal:
angular.module('starter')
.controller('NewsCtrl', function($scope, content, $cordovaSocialSharing, $timeout, $sce, $ionicModal){
$scope.news = content;
content.getList('comments').then(function (comments) {
$scope.comments = comments;
});
$scope.addComment = function() {
};
$scope.shareAnywhere = function() {
$cordovaSocialSharing.share("Guarda questo articolo pubblicato da DDay", "Ti stanno segnalando questo articolo", content.thumbnail, "http://blog.nraboy.com");
};
$ionicModal.fromTemplateUrl('templates/comments.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.showComment = function() {
$scope.modal.show();
};
// Triggered in the login modal to close it
$scope.closeComment = function() {
$scope.modal.hide();
};
$scope.$on('modal.shown', function() {
var footerBar;
var scroller;
var txtInput;
$timeout(function() {
footerBar = document.body.querySelector('#commentView .bar-footer');
scroller = document.body.querySelector('#commentView .scroll-content');
txtInput = angular.element(footerBar.querySelector('textarea'));
}, 0);
$scope.$on('taResize', function(e, ta) {
if (!ta) return;
var taHeight = ta[0].offsetHeight;
if (!footerBar) return;
var newFooterHeight = taHeight + 10;
newFooterHeight = (newFooterHeight > 44) ? newFooterHeight : 44;
footerBar.style.height = newFooterHeight + 'px';
scroller.style.bottom = newFooterHeight + 'px';
});
});
});
I have added this same code in 6 controllers.
Is there a way to avoid the repetition?
Probably what you are looking for is an angular service. This component is a singleton object, that you inject in every controller you need to execute this code.
Angular Services
Regards,
Below is an example of a service I created to retrieve address data from a Json file. Here is the working Plunk. http://plnkr.co/edit/RRPv2p4ryQgDEcFqRHHz?p=preview
angular.module('myApp')
.factory('addressService', addressService);
addressService.$inject = ['$q', '$timeout', '$http'];
function addressService($q, $timeout, $http) {
var addresses = [];
//console.log("Number of table entries is: " + orders.length);
var promise = $http.get('address.data.json');
promise.then(function(response) {
addresses = response.data;
// console.log("Number of table entries is now: " + orders.length);
});
return {
GetAddresses: getAddresses
};
function getAddresses() {
return $q(function(resolve, reject) {
$timeout(function() {
resolve(addresses);
}, 2000);
});
}
}
Here's an example of how I added dependencies for it and another service to my controller (This is NOT the only way to do dependency injection, but is my favorite way as it is easier to read). I then called my addressService.GetAddresses() from within my controller.
var app = angular.module('myApp', ['smart-table']);
app.controller('TableController', TableController);
TableController.$inject = [ "orderService", "addressService"];
function TableController( orderService, addressService) {
addressService.GetAddresses()
.then(function(results) {
me.addresses = results;
// console.log(me.addresses.length + " addresses");
},
function(error) {})
.finally(function() {
me.loadingAddresses = false;
});
});}
I also had to include my .js tag in a script element on my index.html.
<script src="addressdata.service.js"></script>

angularJS service not getting called

I'm very new to AngilarJS. I am trying to write a service in angularJS.
<script>
var module = angular.module("myapp", []);
module.service('BrandService', function ($http) {
var brands = [];
this.getBrands = function()
{
return $http.get('http://admin.localhost/cgi-bin/brand.pl')
.then(function(response)
{
brands = response.brands;
alert (brands);
});
}
//simply returns the brands list
this.list = function ()
{
return brands;
}
});
module.controller("brandsController", function($scope, BrandService) {
$scope.brandlist = BrandService.list();
alert ($scope.brandlist);
});
</script>
The statement "alert (brands);" is not getting called. What is the issue with this code. Is m missing any thing in implementation?
$http calls are always async. Meaning, even you do a .then at your service, there is no way it will properly the resolved data back into your controller. You will have to write it in your controller.
Your Service:
module.service('BrandService', function($http) {
var brands = [];
this.getBrands = function() {
//do not need the dot then.
return $http.get('http://admin.localhost/cgi-bin/brand.pl')
}
//simply returns the brands list
this.list = function() {
return brands;
}
});
In your controller:
module.controller("brandsController", function($scope, BrandService) {
BrandService.list()
.then(function(response) {
$scope.brandlist = response.brands;
alert($scope.brandlist);
});
});
In service:
this.getBrands = function() {
$http.get('http://admin.localhost/cgi-bin/brand.pl').then(function(response) {
brands = response.brands;
alert(brands);
return brands;
});
}
In controller:
$scope.brandlist = BrandService.getBrands();
alert($scope.brandlist);

Resources