In angularjs when i click on button,then it will call function of Controller but in that function i am not able to get $scope or $rootScope.
Here is link :
https://plnkr.co/edit/rFDGLPMdvW4BeTBdu5s8?p=preview
when you click on Go To Store.. store page will open.. and click on Add to Cart.. then $scope function will call..but not getting $scope in that functionenter code here
You have a top-level controller called 'MainController'. The Store.html view uses this controller, but the AddToCart function is located in the StoreController. You need to add the Store controller to the store view (top line of store.html):
<div ng-controller="StoreController">
Updated plunkr
Your app is working fine and if you open Developer tools you will see that the debugger breakpoint is being hit. The issue that I found is that you don't have the route defined for Cart. Also the $scope.cartItemCount won't be auto-updated
Related
This HTML button code is in controller:
On click of a button, trying to call this function:
<button ng-click="vm.openpopup()" ng-if="!vm.data.length"
uib-tooltip="Add Business Value Chain"
class="btn btn-default `enter code here`ti-form-action-btn" id="add-bvc-btn">
<em class="glyphicon glyphicon-plus-sign"></em>
Add
</button>
function openpopup() {
$scope.$broadcast('popup');
}
Below is the broadcast listener code which is inside component under the same controller mentioned above:
$scope.$on('popup', function () {
openModalPopup();
});
The button is displayed only when there is no data present.
Function call is absolutely fine, but broadcast works only once if there is data and if those data are deleted manually, then button gets displayed and broadcast works. But for on page load, if no data present. Broadcast not getting triggered.
Tried using $rootScope.broadcast, still no luck.
Also checking with some other answers, binded the block of code inside $timeout, still no results.
So this is a communication between controller and component using broadcast. How to handle this on load?
I think the issue in your case you have controlled the visibility of your button with ng-if, try it using ng-show instead of ng-if. In case of ng-if there is a chance that the template not to load. I hope that will solve your problem.
I am very New to AngularJS and I have an web Application that calls an ng-click when a button is clicked. I would like to add code to call this function on page load.
This is the where the function is called in the code. Is there anyway to do this?
When element is loaded you can fire ng-init
<div ng-init="zoom.fit()">
some code
</div>
ng-init documentation
Use this in your controller
$scope.$on('$viewContentLoaded', function() {
$scope.zoo.fit();
});
I have a simple App built with Ionic/Cordova. In each page I want to create a simple "Go Back" block button - the user can press it to go to the previous page in the App.
I'm thinking of doing this using $ionicHistory. However, the method $ionicHistory.goBack() is not working.
I am currently using the usual window.history.back() instead, which works, but I don't understand why the ionic method is not working like it should.
Here is the view of the code:
<button class="button button-block button-assertive" ng-click="goBackHandler()">
Go Back
</button>
And here is the Controller:
angular.module('starter.controllers', ['ionic'])
.controller('AppCtrl', function($scope, $ionicHistory)
{
$scope.goBackHandler = function()
{
$ionicHistory.goBack(); //This doesn't work
//window.history.back(); //This works
//alert('code to go back called. Did it work?'); //For testing
}
});
This should be pretty straight-forward. What could I be missing?
Edit: Plunker here - http://plnkr.co/yJqdfs
$ionicHistory.goBack() works only when you have routing and when you navigate to another screen using $state.go('testPage').
In the plunker you've
Go to Test Page
Which is a general browser redirection, which IonicHistory can't help you.
Ionic History will work based on the states stack it has.
Because when you redirect using href="test.html", there will be NO state pushed to Ionic History, so $ionicHistory.goBack() couldn't find any back state.
Please use this one. This works for me.
window.history.back();
i have a normal navbar with a menuController, this is the parent.
then i have two other controllers a DashBoardController this is loaded on startup and a LoadDataController this is called if i click a Link in my navbar.
it do the routing with the routeprovider like this:
$routeProvider.when('/', {templateUrl: 'partials/dashboard.html', controller: 'DashBoardController'})
$routeProvider.when('/loadData', {templateUrl: 'partials/loadData.html', controller: 'LoadDataController'})
in my LoadDataController i have a function for example doSomething();.. and if i click on a link in my navbar it routes me to the loadData site.. there it should call the doSomething() function. i want not use ng-init i want do this with an broadcast event! The problem is now, if i click on the Link in my navbar, it delegates me to the loadData.html site and sends a broadcast event, but at this moment the $on method in my LoadDataController is not initialized so the method is never called. How can i solve this problem ? is it possible that i can initialize my DashBoard an LoadDataController on startup?so that both controllers the DashBoard and my LoadDataController are initialized if i enter my index site?
EDIT:
#Dayan no i want not call a special function every time at initialization! my menucontroller is initalized at startup and is the parent controller. below this controller are two other controllers my DashBoard and my LoadDataController. In my Navbar i have different links that should call different functions in my LoadDataController. The problem is now, that the broadcast event did not reach the LoadDataController because at this moment the $on method is not initialized.. for example.. i have in my navbar a link that calls a function in my navbarcontroller like this:
$rootScope.$broadcast('callFunctionInLoadDataControllerEvent', null);
$location.path('/loadData');
and in my LoadDataController i have the $on method that shoud call a special function like this:
$scope.$on('callFunctionInLoadDataControllerEvent', function(scope, data) {
$scope.specialFunction();
}
the problem is now, i send my broadcast and at this time the LoadDataCOntroller is not initialized(and so the $on mehtod is also not initialized, the event never reaches my controller), the initizations starts first, if i change the path with $location.path('/loadData');
I would like to execute Controller code when a Tab is close.
For that I used Controller action with a ControlQuery.
The query seems to be right but the action never fired...
Here is my fiddle: http://jsfiddle.net/charlesbourasseau/sU7Me/
What am I doing wrong ?