how to call angular scope function from javascript function inside controller
Declare $scope.Name
and $scope.dates
on top of validation()
Javascript works from top to bottom, so your functions $scope.Name
and $scope.Dates
do not exist 'yet'.
Also, try not to use 'Name' as a function. Most of these words are reserved keywords.
var myApp = angular.module('myApp', []);
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});
function MyCtrl($scope) {
$scope.Name = function() {
// do something
}
$scope.dates = function() {
// do something
}
function validation() {
$scope.pageload = true;
$scope.Name();
$scope.dates();
}
}
Fiddle: http://jsfiddle.net/Lvc0u55v/4872/
An even better approach would be the 'John Papa style' : Y033
Place bindable members at the top of the controller, alphabetized, and not spread through the controller code.
Why?: Placing bindable members at the top makes it easy to read and helps you instantly identify which members of the controller can be bound and used in the View.
Why?: Setting anonymous functions in-line can be easy, but when those functions are more than 1 line of code they can reduce the readability. Defining the functions below the bindable members (the functions will be hoisted) moves the implementation details down, keeps the bindable members up top, and makes it easier to read.
/* avoid */
function SessionsController() {
var vm = this;
vm.gotoSession = function() {
/* ... */
};
vm.refresh = function() {
/* ... */
};
vm.search = function() {
/* ... */
};
vm.sessions = [];
vm.title = 'Sessions';
}
/* recommended */
function SessionsController() {
var vm = this;
vm.gotoSession = gotoSession;
vm.refresh = refresh;
vm.search = search;
vm.sessions = [];
vm.title = 'Sessions';
////////////
function gotoSession() {
/* */
}
function refresh() {
/* */
}
function search() {
/* */
}
}
Mohamed Sahir
Updated on July 09, 2022Comments
-
Mohamed Sahir almost 2 years
I have angular controller and Javascript function in that function , i am calling angular function. I am getting error: $scope.Name is not a function, $scope.dates is not a function.
function validation() { $scope.pageload = true; $scope.Name(); $scope.dates(); } $scope.Name = function () { // do something } $scope.dates = function () { // do something }
working fine inside the controller
var MyController = function ($scope, service) { function validation() { $scope.pageload = true; $scope.Name(); $scope.dates(); } $scope.Name = function () { // do something } $scope.dates = function () { // do something } }); working: var MyController = function ($scope, service) { LoginHomeService.getHomeService(function (data) { $rootScope.CT1SessionObj = data.CT1SessionObj; validation(); }, function (response) { alert(response.Message); }); function validation() { $scope.pageload = true; $scope.Name(); $scope.dates(); } $scope.Name = function () { // do something } $scope.dates = function () { // do something }); Not working: var MyController = function ($scope, service) { LoginHomeService.getHomeService(function (data) { $rootScope.CT1SessionObj = data.CT1SessionObj; validation(); function validation() { $scope.pageload = true; $scope.Name(); $scope.dates(); } $scope.Name = function () { // do something } $scope.dates = function () { // do something } }, function (response) { alert(response.Message); }); });
-
Anubrij Chandra almost 7 yearsIf you want to mention someone , use comment instead of answer