angularjs routing without links - html

I have two problems.
First my test.html doesn't "see" the products array from his controller.
I don't know why, I have connected the controller with the html in the routing.config.js.
var myapp = angular.module('myapp', ["ui.router", "ngAnimate"]);
myapp.config(function($stateProvider, $urlRouterProvider){
$stateProvider
.state("foo", {
url: "/foo",
templateUrl:'start.html'
})
.state("test", {
url: "/test",
templateUrl: 'test.html',
controller:'product-controller',
parent:'foo'
})
$urlRouterProvider.otherwise("/foo");
})
myapp.controller('navCtrl', function($scope, $state){
$scope.navigateTo = function(target){
$state.go(target);
}
});
the second thing I want is that the test.html will be activated without the click on the link.
If the mainpage is loaded, the test.html should be loaded too.
At the end I want to delete the <ul> but the result should be the same.
(the foo and the dropdown).
I have made a PLUNK where you can see the problem.
The only solution I had, is to integrate the test.controller.js in the routing.config.js and the test.html in the start.html.
But i hope to avoid this because it makes the code more confusing.
I also looked at the ui-router docs but it doesn't work for me.
.state("foo.test", {
url: "/test",
templateUrl: 'test.html',
controller:'product-controller',
parent:'foo'
})
I hope someone has an idea... thank you! =)

I would take #charlietfl's advice and read up on nested views. Other than that, you had a few errors in your plunkr:
Forgot to load test-controller.js in index.html
Forgot to inject the product-selection module to myapp (so it can resolve the respective controller)
Forgot to add the ngModel to the select element in test.html for data binding to work as expected
Here's your plunkr updated with the select control populated properly.

Related

AngularJS custom directive scope error

I am having hard time to figure out why scope inside directive does not work. What I am doing wrong. Program should be able to remove an item by clicking button, but scope does not work, if I comment out scope in directive data is there otherwise nothing is happening. User is supposed to search for an item in menu and based on criteria items will be shown, for example enter chicken and number of items should show, beside each item should show up button and by pressing that button that item should be removed. This does not work.
if anyone can have look and give me an answer what is wrong I'd kindly appreciate. By the way I am new to AngularJS and trying to learn.
function FoundItemsDirective(){
var ddo = {
scope: {
//items: '<',
//myTitle: "#",
onRemove: '&'
//visible: '='
},
templateUrl: 'foundItems.html'
// controller: NarrowItDownDirectiveController,
// controllerAs: 'narrowDown',
// bindToController: true
};
return ddo;
}
Here is link to plunk http://plnkr.co/edit/s9Fh4RkXIhrhLoVv8ZT2?p=preview
Thanks to everyone who tried to help, I figure out by myself this one.
function FoundItemsDirective(){
var ddo = {
templateUrl: 'foundItems.html',
scope: {
found: '<',
onRemove: '&'
},
controller: NarrowItDownDirectiveController,
controllerAs: 'narrowDown',
bindToController: true
};
return ddo;
}
Here is plunk http://plnkr.co/edit/s9Fh4RkXIhrhLoVv8ZT2?p=preview

Store HTML page in memory with Angular2

I would like to keep web page in memory so that when I click on back button (not the one on web browser) or on a routerlink, the HTML page instantly loads if I already visit it(because I have some data to load that I don't want to be reload).
I've seen a method with tabbed interface : https://www.w3.org/Style/Examples/007/target.en.html#tab1
but it is not adapted for my code architecture.
I'm using routerlinkin angular2 to navigate threw pages and a back button calling a function on click to go to the previous page.
I try to detail as far as I can so people can understand better my code architecture and the way routerlink method works.
Back button function (works independently from routerlink) :
goBack() {
window.history.back();
}
The router link method from page 1 to page 2:
page1.html :
<a[routerLink]="['PAGE2']"> go to page 2</a>
page1.ts component:
import { Router, ROUTER_DIRECTIVES } from '#angular/router-deprecated';
#Component({
selector: 'page1',
templateUrl: 'page1.html',
styleUrls: ['page1.css'],
directives: [ROUTER_DIRECTIVES]
})
main.ts :
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '#angular/router-deprecated';
#Component({
providers: [ROUTER_PROVIDERS]
})
#RouteConfig([
{ path: '/page2', name: 'PAGE2', component: Page2}]) //component representing class Page2 in page2.ts
Any idea to help me manage it is welcomed, thanks by advance !
Just cache the data in the service like explained in What is the correct way to share the result of an Angular 2 Http network call in RxJs 5?
There is currently no way to prevent the router from re-creating the component when you route away and back to the component.
Well, for those having the same problem, I think the best way to manage it is to map the data into a localStorage key like this :
localStorage.setItem('favoris',JSON.stringify(my_array)); //set my data array
array = JSON.parse(localStorage.getItem('key_name')); //get in array
And then ngOnInitin the class called by the router will call the initial function depending of localStorage key being true or not.

ModalInstance updating Image from upload

I am trying to upload an image from a drag and drop feature. I have set the modal and my drag and drop feature fine, but what I am having an issue is when I upload the file I would like to update the img src. I have a simplified version for my controller.
app.controller("CreateUploadInstanceCtrl", ["$scope", "$uibModalInstance",
function ($scope, $uibModalInstance) {
this.image = "/path";
}]);
In my modal call I have the following:
var modalInstance = $uibModal.open({
templateUrl: 'app/views/forms/upload_profile_picture.html',
controller: 'CreateUploadInstanceCtrl',
}
});
In my Html I would assume that I could just call
<img ng-src="{{image}}"/> to get the image to show on upload but I see nothing firing. I am not sure how the this.image can be translated to the html side since I can't load a controller into the html view. Would I need a resolve and if so how would I use the resolve?
Any help would be great.
Instead of this.image, try using $scope.image and in html specify ng-controller="CreateUploadInstanceCtrl"

ionic - $state.go with params issue1

I just create the ionic project and I'm trying to make the sign in and sign up page and I just implement the HTML and CSS. but the problem is I can't change position between controllers.
The URL of controller is changes but the page is not changed. I was trying to import the correct module but I can't find the method.
Make sure in app.js, your templateURL , url is properly defined.
Most of the time it might be your 'URL' problem.
If you navigated from 'main', your next url should be : /main/success or something like that.
.state('tab.main', {
cache: false, //if you want to disable cache in ionic
url: '/main',
views: {
'tab-cases': {
templateUrl: 'templates/tab-main.html',
controller: 'MainCtrl'
}
}
})

Generate Page Based on JSON Data

I'm using AngularJS for an app I'm building and was wondering if it's possible to generate pages when items are pushed to a JSON object called places. Each item when pushed is given a unique ID and I figured I could use this id (e.g. 123456) as part of the url like so site.com/places/123456.
{
"places" : [
{
"id" : 471756,
"title" : "The Whittington Hospital"
}
]
}
Is it possible to have this page generated automatically (for example, based on a template)?
I ask because I'm trying to build an app that let's users create their own hospitals via a form. Once a hospital is created and pushed to the JSON object, I'd like a page to be created for that hospital.
Can I use Angular for this? Any help is appreciated.
Thanks in advance!
UPDATE
Still having a bit of trouble with this. Here's what I've got so far.
place.html
<div ng-controller='PlaceCtrl'>
<h1>{{ title }}</h1>
</div>
JS
app.constant('FBURL', 'https://luminous-fire-8685.firebaseio.com/');
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/places/:placeId', {
templateUrl: 'views/place.html',
controller: 'PlaceCtrl'
})
}]);
app.factory('place', function($firebase, FBURL, $routeParams) {
var ref = new Firebase(FBURL + "places/" + $routeParams.placeId);
return {
id: $routeParams.placeId
}
});
app.controller('PlaceCtrl', function($scope, $rootScope, FBURL, $firebase, $location, $routeParams, place) {
$scope.placeId = place.id;
});
You're looking for ngRoute and $routeProvider. With those you can set up a parameterized route like this:
.when('/place/:placeId', { templateUrl: 'place.html', controller: 'PlaceCtrl' })
So every time a visitor hits a URL that starts with /place/ it ends up at PlaceCtrl and place.html. And the place ID is passed as a parameter.
Then you can pick up the parameter in your controller:
app.controller('PlaceCtrl', function($scope, FBURL, $firebase, $routeParams) {
var ref = new Firebase(FBURL+"places/"+$routeParams.placeId);
See also:
My sample application that uses AngularJS, Firebase and AngularFire: https://github.com/puf/trenches/blob/master/app.js (from which I copy/paste/modified the above snippets)
The AngularJS documentation for routeParams
The AngularJS tutorial's step on routing
The (way better) Thinkster.io tutorial page on routeParams