I have trouble to catch a custom event that fired from my-login Element with a button to the my-overview Element. I really have no clue why this is not working.
index.html
<iron-pages attr-for-selected="data-route" selected="{{route}}">
<section data-route="login">
<paper-material id="pmLogin" elevation="1">
<paper-toolbar>
<h1 class="paper-font-display1"><span>Login</span></h1>
</paper-toolbar>
<my-login id="elLogin"></my-login>
</paper-material>
</section>
<section data-route="overview">
<paper-material id="pmOverview" elevation="1">
<paper-toolbar>
<h1 class="paper-font-display1"><span>Overview</span></h1>
</paper-toolbar>
<my-overview id="elOverview"
on-call-overview-refresh="reloadOverview">
</my-overview>
</paper-material>
</section>
</iron-pages>
my-login.html
routeTo: function(route) {
var app = document.querySelector('#app');
app.route = route;
this.fire('call-overview-refresh');
},
my-overview.html
reloadOverview: function() {
...
}
In short, you need to declare the on-* event handler on the element that actually fired the custom event - in this case <my-login>.
Seeing that your <iron-pages> is in index.html, I suppose the markup is wrapped inside a <template is="dom-bind"></template>? If that's the case, your index.html might look something like this:
<template id="app" is="dom-bind">
...
<iron-pages attr-for-selected="data-route" selected="{{route}}">
<section data-route="login">
<paper-material id="pmLogin" elevation="1">
<paper-toolbar>
<h1 class="paper-font-display1"><span>Login</span></h1>
</paper-toolbar>
<my-login id="elLogin"
on-call-overview-refresh="callReloadOverview"></my-login>
</paper-material>
</section>
<section data-route="overview">
<paper-material id="pmOverview" elevation="1">
<paper-toolbar>
<h1 class="paper-font-display1"><span>Overview</span></h1>
</paper-toolbar>
<my-overview id="elOverview"></my-overview>
</paper-material>
</section>
</iron-pages>
...
</template>
<script>
window.addEventListener("WebComponentsReady", function (e) {
var app = document.querySelector("#app");
app.callReloadOverview = function () {
app.$.elOverview.reloadOverview();
}
...
});
</script>
In the above snippet, when <my-login> fires the call-overview-refresh event, the callReloadOverview() function will be called, which in turns calls <my-overview>'s reloadOverview() method.
Related
When I call ngDialog no information that is in the template is displayed. It arrives to the DOM, but it isn't displayed.
This is the function where it is called:
controller.showPlan = function (plan) {
ngDialog.open({
template: 'client/src/organizer/app/planner/plan.html',
scope: $scope.this
});
};
This is the template:
<script type="text/ng-template">
<div class="ngdialog-message">
<h1>Template heading</h1>
<p>Content goes here</p>
</div>
This is the place in the main template where it is called:
<label ng-if="Planner.hasPlan(plan)" ng-click="Planner.showPlan(plan)">{{plan.title}}</label>
Everything works except ngDialog.
Add an id to your text/ng-template that matches the url of the template. So this template will be written to $templateCache with the name client/src/organizer/app/planner/plan.html & when you request the template it will be read from there ($templateCache).
HTML
<script type="text/ng-template" id="client/src/organizer/app/planner/plan.html">
<div class="ngdialog-message">
<h1>Template heading</h1>
<p>Content goes here</p>
</div>
</script>
Html5 dialogs are simple. Shoulda been there 15 years ago!
How to get ng-show working (it doesn't) with dialogs?
<!DOCTYPE html>
<html ng-app="project">
<head>
<meta charset='UTF-8'>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
<script>
angular.module('project', [])
.controller('TheController', function ($scope) {
$scope.dialogShowing = false;
$scope.showDialog = function () {
dialogShowing = true;
document.getElementById('theDialog').show();
};
$scope.hideDialog = function () {
dialogShowing = false;
document.getElementById('theDialog').close();
};
});
</script>
</head>
<body ng-controller="TheController">
<div> Hello
<!-- dialog, where it is placed in the source, doesn't take up space -->
<dialog id="theDialog">
<div>
<h3>Simple Angular Html5Dialog</h3>
<hr/>
<p>I am very well, thank you</p>
<button ng-click="hideDialog()">No, thank you</button>
</div>
</dialog>
how are you?
</div>
<button ng-click="showDialog()">^ Click for the answer</button>
</body>
</html>
The only thing I have been able to get working is .open() and .close() on the dialog widget itself, and have to simulate ng-show/hide, above.
Advice?
I'm not sure what browser you are using, but dialog is very much unsupported by most browsers (http://caniuse.com/#search=dialog). If it's a simple modal you're looking to create why not use a div and style it accordingly?
It's perfectly possible. Here's a demo from my blog:
<div style="width: 600px;" id="example">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script type="text/javascript">
var App = angular.module("MyApp", []);
// Make ng-show/hide doesn't work with <dialog>
App.controller('DialogDemo', ['$scope', function ($scope) {
$scope.dialogShowing = false;
$scope.showDialog = function () {
$scope.dialogShowing = true;
$scope.whatHappened = "something happened that needs a dialog";
document.getElementById('theDialog').showModal();
};
$scope.hideDialog = function () {
$scope.dialogShowing = false;
document.getElementById('theDialog').close();
$scope.whatHappened = undefined;
};
}]);
</script>
<div style="align-content: center" ng-app="MyApp" ng-controller="DialogDemo">
(text above button)<br/>
<button ng-click="showDialog()">Click me</button><br/>
(text below button, and above dialog in source)<br/>
<dialog id="theDialog">
<div>
<h3>Ooops</h3>
<hr/>
<div>
<!-- see note above about double-curly-brace -->
{{whatHappened}}
</div>
<hr/>
<button ng-click="hideDialog()">OK</button>
</div>
</dialog>
(text below dialog in source)<br/>
<span style="font-weight: bold" ng-show="dialogShowing">Dialog should now be show (modal style) in the center of the page.</span>
</div>
</div>
I have the following Angular code:
index.html
<body>
<main ng-app="app">
<div class="container">
<div class="parent" ng-controller="ParentController">
<p>{{person.name}}</p>
<p>{{person.email}}</p>
<div class="child" ng-controller="ChildController">
<div class="">
<p><input type="text" ng-model="email"></p>
<button ng-click="updateEmail(email)" name="button">Submit Email</button>
</div>
<p>{{person.name}}</p>
<p>{{person.email}}</p>
</div>
</div>
</div>
</main>
<script src="scripts\lib\angular.min.js" charset="utf-8"></script>
<script src="scripts\controllers\ParentController.js" charset="utf-8"></script>
</body>
and app.js
var app = angular.module("app", []);
app.controller("ParentController", ParentController);
app.controller("ChildController", ChildController);
ParentController.$inject = ["$scope"];
ChildController.$inject = ["$scope"];
function ParentController($scope) {
$scope.person = {
name: "Name"
};
}
function ChildController($scope) {
$scope.person.email = "Not the real email";
$scope.updateEmail = function(theEmail) {
$scope.person.email = theEmail;
};
}
I expected to see "undefined" dipslayed on the page for the {{person.email}} that's inside ParentController and outside ChildController, but this isn't what happened. When the page is loaded, I can see my placeholder value in both the ParentController and ChildController. If the property hasn't been set on the object yet, how can ParentController see the value of that's set in the ChildController. I know ChildController should be able to see everything in ParentController because of inheritance, which is exactly what I was testing out. But why can ParentController see the value of person.email?
I have the following structure:
<html>
<head>
// additional info here
</head>
<body data-ng-app="myApp" data-ng-controller="contentController">
<div id="container">
<div id="id1">
//content here
</div>
<div id="id2">
//content here
</div>
<div id="id3">
//content here
</div>
<div id="page-content">
<div data-ng-view="">
//here will be loaded the other views
//Example: /profile, /login, /register, etc etc)
</div>
</div>
</div>
</body>
</html>
What I need is to hide the divs: id1, id2, id3 when the user navigates to specific pages like /login or register. For all other pages the divs: id1, id2, id3 should be visible.
At the moment when the user navigates to /login the divs: id1, id2, id3 content is shown with the login form so I have to hide it somehow.
The divs: id1, id2, id3 are common for all pages except for /login, /register and /forgot.
You can use the $locationChangeSuccess event broadcasted from the $rootScope to check the current route using the $route service. The advantage of this methodology, is that navigation through the use of the address bar can still be detected.
DEMO
JAVASCRIPT
.controller('contentController', function($scope, $rootScope, $route) {
var paths = ['/login', '/register', '/forgot'];
$rootScope.$on('$locationChangeSuccess', function() {
var $$route = $route.current.$$route;
$scope.contentVisibility = $$route && paths.indexOf($$route.originalPath) < 0;
});
});
HTML
<body data-ng-app="myApp" data-ng-controller="contentController">
<div id="container">
<div id="id1" ng-show="contentVisibility">
//content here
</div>
<div id="id2" ng-show="contentVisibility">
//content here
</div>
<div id="id3" ng-show="contentVisibility">
//content here
</div>
<div id="page-content">
<div data-ng-view="">
//here will be loaded the other views
//Example: /profile, /login, /register, etc etc)
</div>
</div>
<!-- list of routes bound within the anchor tags -->
<a ng-repeat="path in [
'/login', '/register', '/forgot',
'/other-route-1', '/other-route-2', '/other-route-3']"
ng-href="#{{path}}">{{path}}<br></a>
</div>
</body>
you should use data-ng-hide to hide your contents. however, you have to set the variable at start of your specific controller in order to hide the contents and in other controllers you should set it false to show the contents.
At the beginning of your specific controller:
var $scope.contentHide = true;
At the beginning of other controllers:
var $scope.contentHide = false;
<div id="id1" data-ng-hide="contentHide">
//content here
</div>
<div id="id2" data-ng-hide="contentHide">
//content here
</div>
<div id="id3" data-ng-hide="contentHide">
//content here
</div>
I've tried
$stateChangeSuccess,
$routeChangeSuccess, and
$viewContentLoaded
but I can't get my custom jQuery init function to run after my ng-include loads or if I switch it after the fact with an ng-swith.
Index.html has ng-view to dashboard.html which has this code.
<div class="row">
<div class="col-md-12">
Switch Dashboard View: <input type="radio" name="dashboardView" value="0" ng-model="vm.dashboardView" />Franchisee |
<input type="radio" name="dashboardView" value="1" ng-model="vm.dashboardView" />Real Estate
</div>
</div>
<!--tiles end-->
<div data-ng-switch="vm.dashboardView">
<div data-ng-switch-when="0">
<div data-ng-include="'/Views/Dashboard/FranchiseeDashboard.html'"></div>
</div>
<div data-ng-switch-when="1">
<div data-ng-include="'/Views/Dashboard/RealestateDashboard.html'"></div>
</div>
</div>
index.html has a controller which has this
$rootScope.$on("$viewContentLoaded", function () {
Interactions.init();
});
$rootScope.$on('$stateChangeSuccess',
function (event, next, current) {
Interactions.init();
}
);
$rootScope.$on('$routeChangeSuccess',
function (event, next, current) {
Interactions.init();
}
);
How can I get my jquery function to fire after ng-switch has loaded an ng-include?
This should work $viewContentLoaded and includeContentLoaded
Look at the events for ngInclude
https://docs.angularjs.org/api/ng/directive/ngInclude