I have the following code which displays a static JSON object in a grid.
var app = angular.module('app', ['ui.bootstrap']);
app.controller('ModalInstanceCtrl', function($scope, $modalInstance, customer) {
$scope.customer = customer;
});
app.controller('CustomerController', function($scope, $timeout, $modal, $log) {
$scope.customers = [{
name: 'Movie Title 1',
details: 'http://image.tmdb.org/t/p/w342//lFSSLTlFozwpaGlO31OoUeirBgQ.jpg',
}, {
name: 'Movie Title 2',
details: 'http://image.tmdb.org/t/p/w342//tgfRDJs5PFW20Aoh1orEzuxW8cN.jpg',
}, {
name: 'Movie Title 3',
details: 'http://image.tmdb.org/t/p/w342//wJXku1YhMKeuzYNEHux7XtaYPsE.jpg',
}];
// MODAL WINDOW
$scope.open = function(_customer) {
var modalInstance = $modal.open({
controller: "ModalInstanceCtrl",
templateUrl: 'myModalContent.html',
resolve: {
customer: function() {
return _customer;
}
}
});
};
});
I need to be able to use this RESTful API source:
$http.get("http://api.themoviedb.org/3/movie/now_playing?api_key=ebea8cfca72fdff8d2624ad7bbf78e4c")
.success(function(response) {
console.log(response);
$scope.results = response.results;
});
and enable the click event to trigger the modal as it does now, except it needs to grab the details of each of the items in the JSON object and display in the modal.
The ng-repeat:
<div class="container">
<div class="row">
<div ng-repeat="items in results">
<img class="col-lg-3 col-md-3 col-sm-3 col-xs-12 thumbnail" ng-src="http://image.tmdb.org/t/p/w342/{{items.poster_path}}">
</div>
</div>
</div>
Using this fiddle: http://jsfiddle.net/8s9ss/224/ as a base, I need to have the buttons replaced by the images coming through the REST API and they should trigger a modal on click.
This should be all you need
<div ng-repeat="items in results">
<a ng-click="open(items)" class="col-lg-3 col-md-3 col-sm-3 col-xs-12 thumbnail">
<img ng-if="items.poster_path" ng-src="http://image.tmdb.org/t/p/w342/{{items.poster_path}}">
<div class="caption" ng-hide="items.poster_path">
<h3>{{items.title}}</h3>
</div>
</a>
</div>
You'll need to tweak your modal template to show the different properties but you should be able to figure it out.
http://jsfiddle.net/8s9ss/229/
Related
I want to display the clicked row button details info in next view,I am displayin only code,nom in first page and remaining fields will view after clicking button.
I used the option "filter" to do it, but sometimes it returns details of non concerned code , like for the two codes: 45 and 453 , it gaves the same detail because of the common number '45'.
First html page:
<div class="row header">
<div class="col" >Code</div>
<div class="col">Client</div>
</div>
<div class="row" ng-repeat="x in namesC">
<div class="coll">
<a class="button" href="#/detail/{{x.Code}}">
{{x.Code}}</a>
</div>
<div class="col"> {{x.NomClient}} </div>
</div>
second html page (detail.html):
<ion-list ng-repeat="x in namesC| filter: {Code: thisX}|limitTo:1">
<ion-item>
<div class="item item-divider center-text"> {{x.Code}}</div>
</ion-item>
<ion-item>
<b>adresse</b> <span class="item-note"> {{x.adresse}} </span>
</ion-item>
</ion-list>
app.js :
$stateProvider.state('detailColis', {
url: '/detail/:Code',
templateUrl: 'templates/detail.html',
controller: 'SuiviAdminCtrl'
});
You must add Config file Or add the down code in html file to solve this problem
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl : "main.htm"
})
.when("/detail/:Id", {
templateUrl : "detail.htm"
})
});
</script>
I tested the above code, its working.
We can use $routeParams, Also we can use service. See basic example and try like below.
var app = angular.module('exApp',['ngRoute']);
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider){
$routeProvider
.when('/', {
template:'<p>All Datas View</p><p ng-repeat="data in datas">{{data.name}} <button ng-click="setData(data)">View</button> routeParams.. <button ng-click="setrouteData($index)">Index</button></p>',
controller: 'ctrl',
})
.when('/detail',{template:'<p>Particular Data View</p><button ng-click="back()">Back</button><br><p>{{data}}</p>',controller: 'ctrlOne'})
.when('/detail/:id',{template:'<p>Data View</p><button ng-click="back()">Back</button><br><p>{{data}}</p>',
controller: 'ctrltwo'})
.otherwise({redirectTo:'/'});
}]);
app.controller('ctrl', function($scope,$location,exService){
$scope.datas = exService.data;
$scope.setData = function(data){
//console.log(data);
exService.Obj = data;
$location.path('detail');
}
$scope.setrouteData = function(index){
$location.path('detail/'+ index);
}
});
app.controller('ctrlOne', function($scope,$location, exService){
var data = exService.Obj;
$scope.data=data;
$scope.back = function(){
$location.path('/');
}
});
app.controller('ctrltwo', function($scope,$location, $routeParams,exService){
var data = $routeParams.id;
$scope.datas = exService.data;
$scope.data=$scope.datas[data];
$scope.back = function(){
$location.path('/');
}
});
app.service('exService', function(){
var Obj = {};
var data = [{"id":1, "name":"xxx"},{"id":2, "name":"yyy"},{"id":3, "name":"zzz"}];
return { Obj, data};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular-route.min.js"></script>
<body ng-app="exApp">
<p>Sample</p>
<div ng-view></div>
</body>
this is the solution, Thanks to Sudarshan_SMD
The solution is just to change filter: with :
filter: {CodeEnvoiColis: thisX}:true
I Need to use same data on Two different Controller, One is page while other is Modal Popup. I created service which will perform $http.get to pull data. However, when I apply service to Modal Popup, it stops loading and even data is not load anywhere.
My Service
(function(){
'use strict';
angular.module('sspUiApp.services')
.service('AdUnitService', ['$http', '$rootScope', 'API_URL', function($http, $scope, API_URL) {
var data = $http.get('data/selectAdUnits.json');
return {
getAdFormats: function() {
console.log("inside function");
return data;
},
setAdFormats: function(value) {
}
}
}]);
})();
My Both Controller with Services added.
(function(){
'use strict';
angular.module('sspUiApp.controllers')
.controller('AdUnitFormatCtrl', ['$scope', '$state', 'AdUnitService', function ($scope, $state, AdUnitService) {
$scope.details = AdUnitService.getAdFormats();
}])
.controller('ModalDemoCtrl', ['AdUnitService', function ($scope, $uibModal, AdUnitService) {
$scope.open = function (size) {
// $scope.details = AdUnitService.getAdFormats();
$scope.$modalInstance = $uibModal.open({
scope: $scope,
templateUrl: 'views/select_ad_format.html',
size: size,
});
};
$scope.cancel = function () {
$scope.$modalInstance.dismiss('cancel');
};
$scope.details = AdUnitService.getAdFormats();
alert($scope.details);
}])
})();
And HTML
<div id="selectAdFormats" ng-controller="ModalDemoCtrl">
<div class="container">
<div class="ad-format-section">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-2 col-xs-6 selectedAdFormatData" ng-repeat="frmt in details.adformat">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
<img ng-src="../images/{{ frmt.ad_image }}" ng-if="frmt.ad_image"/>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
<span class="formatName">{{ frmt.name }}</span>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
<span class="resSize">{{ frmt.size }}</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
angular.module('sspUiApp.services')
angular.module('sspUiApp.controllers')
Your service is in a different module, that's why you can't get to it, if you declare it into the same module it should work
Option 2:
Include your second module sspUiApp.services in your decleration of sspUiApp.controllers
for more info: Angular js seperation to different modules in different js files
Update:
var data = $http.get('data/selectAdUnits.json');
return {
getAdFormats: function() {
console.log("inside function");
return data;
},
setAdFormats: function(value) {}
}
The problem is is that data is asynchronious and you don't treat it like so:
$scope.details = AdUnitService.getAdFormats();
alert($scope.details);
You try to alert a promise, instead of waiting for it to resolve and alerting the result, if you change it into the following it should work:
AdUnitService.getAdFormats().then(function(details) {
alert(details);
});
This way angular will wait on the result before executing the code.
More info about Angular promises here: https://docs.angularjs.org/api/ng/service/$q
I am using angular-formly to build a form inside an angular-ui-bootstrap modal, the following code works when the form is placed outside the modal template but it doesn't when placed inside the ng-template, it just doesn't print the fields at all.
I believe this should work but I don't know how the life-cycle of angular-formly runs, so I am unable to identify how to make the fields show up inside my bootstrap modal template.
The issue is clearly related to the ng-template, it appears not to render the form even if the fields array is passed correctly.
var app = angular.module("demo", ['dndLists', 'ui.bootstrap', 'formly', 'formlyBootstrap']);
app.controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items, User) {
var vm = this;
vm.loadingData = User.getUserData().then(function(result) {
vm.model = result[0];
vm.fields = result[1];
vm.originalFields = angular.copy(vm.fields);
console.log(vm);
});
});
app.controller("AdvancedDemoController", function($scope, $uibModal){
$scope.modalOpen = function(event, index, item){
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: 'md',
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
});
In my view:
<!-- Template for a modal -->
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
<div ng-if="vm.loadingData.$$state.status === 0" style="margin:20px 0;font-size:2em">
<strong>Loading...</strong>
</div>
<div ng-if="vm.loadingData.$$status.state !== 0">
<form ng-submit="vm.onSubmit()" novalidate>
<formly-form model="vm.model" fields="vm.fields" form="vm.form">
<button type="submit" class="btn btn-primary submit-button">Submit</button>
</formly-form>
</form>
</div>
<ul>
<li ng-repeat="item in items">
{{ item }}
</li>
</ul>
Selected: <b>{{ selected.item }}</b>
</div>
</script>
Any ideas?
When calling $uibModal.open you need to specify controllerAs: 'vm' (that's what you're template assumes the controller is defined as).
I am new to angularjs,i make a sample app ,now i want to display data on next page when form is submitted using isolated scope directive .
my index.html:
<body ng-controller="mainController">
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Angular Routing Example</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><i class="fa fa-home"></i> Home</li>
<li><i class="fa fa-shield"></i> About</li>
<li><i class="fa fa-comment"></i> Contact </li>
</ul>
</div>
</nav>
<div id="main">
<!-- angular templating -->
<!-- this is where content will be injected -->
<div ui-view></div>
</div>
</body>
Directive:
var scotchApp = angular.module('scotchApp');
scotchApp.directive('homeData', function() {
return {
scope: {
info: '=fo'
},
templateUrl: 'homeDirective.html'
}
scotchApp.controller('MyFormCtrl', [function() {
this.user = {
name: '',
email: ''
};
this.register = function() {
// console.log('User clicked register', this.user);
alert(this.user.name + "-- " + this.user.email);
$scope.name = this.user.name;
};
}]);
})
how can i make possible this when someone submit the form the about page is displayed after clicking submit button and all form data i want to display there.
here is my plunker: http://plnkr.co/edit/D5S2c6rgycWFfsyfhN9J?p=preview
So based off of what you put into plunker and looking at your code I noticed a few issues.
First: You had your controller inside your directive causing none of the code within the directive to be run.
Your Code
var scotchApp = angular.module('scotchApp');
scotchApp.directive('homeData', function() {
return {
scope:{
info:'=fo'
},
templateUrl: 'homeDirective.html'
}
scotchApp.controller('MyFormCtrl', [function() {
this.user = {
name: '',
email: ''
};
this.register = function() {
// console.log('User clicked register', this.user);
alert(this.user.name+ "-- " +this.user.email);
$scope.name=this.user.name;
};
}]);
})
Update Code:
var scotchApp = angular.module('scotchApp');
scotchApp.directive('homeData', function() {
return {
scope:{
info:'=fo'
},
templateUrl: 'homeDirective.html'
};
});
scotchApp.controller('MyFormCtrl', [function() {
this.user = {
name: '',
email: ''
};
this.register = function() {
// console.log('User clicked register', this.user);
alert(this.user.name+ "-- " +this.user.email);
$scope.name=this.user.name;
};
}]);
Second: You need to add some way allowing your variables to talk between controllers. I used a service and passed it into both controllers
homeDirective.js
scotchApp.controller('MyFormCtrl', ['$scope', 'userService', function($scope, userService) {
$scope.userService = userService;
$scope.user = {
name: '',
email: ''
};
$scope.register = function() {
//sets the variables within the service
$scope.userService.setUserName($scope.user.name);
$scope.userService.setUserEmail($scope.user.email);
alert($scope.userService.getUserName() + "-- " + $scope.userService.getUserEmail());
};
}]);
//Service to be passes to the controllers
scotchApp.service('userService', function(){
var userService = {
user: {
'name': '',
'email': ''
},
getUser: function(){
return userService.user;
},
getUserName: function(){
return userService.user.name;
},
getUserEmail: function(){
return userService.user.email;
},
setUserName: function(name){
userService.user.name = name;
},
setUserEmail: function(email){
userService.user.email = email;
},
};
return userService;
});
script.js
scotchApp.controller('aboutController', ['$scope', 'userService', function($scope, userService) {
$scope.userService = userService;
$scope.user = $scope.userService.user;
$scope.message = 'Look! I am an about page.';
}]);
Third: Your HTML page had to be able to call the register function and move over to the about page.
homeDirective.html
<div class="jumbotron text-center" >
<h1>Home Page</h1>
<p>{{ message }}</p>
<form name="myForm" ng-controller="MyFormCtrl as ctrl">
Name: <input type="text" class="form-control" ng-model="user.name">
Email: <input type="email" class="form-control" ng-model="user.email">
<br/>
<a type="submit" ng-href="#about">
<button ng-click="register();" type="submit">Register</button>
</a>
</form>
</div>
Last: You should put your whole angular app in a self calling function like so
(function(){
var app = angular.module('app'[]);
//more code
})();
Here is the link to the plunk fork that I did for your code. Hopefully that all made sense.Link to Plunker
Good Luck!
Use Angular Services to share data between controllers.You can use services to organize and share code across your app.
I am encountering a strange behavior when using Angular's ng-repeat on a Bootstrap UI modal.
I have this dummy method in my customerService.js factory:
app.factory('customerService', [ function() {
customerFactory.getCustomers =
function () {
return [{ "name": "Terry Tibbs" },
{ "name": "Bobby Halls" },
{ "name": "Garry Brisket" }]
};
return customerFactory;
}]);
This the customerSelectionModal.html modal template:
<div>
<div class="modal-header">
<h3 class="modal-title">Select a customer</h3>
</div>
<div class="modal-body">
<label data-ng-repeat="cust in customers">
<input name="customer" type="radio" value="{{cust}}" ng-model="$parent.selected.item" />{{cust.name}}
</label>
<div ng-show="selected">You have selected: {{ selected }}</div>
<div ng-show="selected">name: {{ selected.item.name }}</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-default" ng-click="cancel()">Cancel</button>
</div>
</div>
</div>
This is the customerController.js file:
'use strict';
appModule.controller('customerController',
['$scope', '$modal', '$log', 'customerService', function ($scope, $modal, $log, customerService) {
$scope.customers = customerService.getCustomers();
$scope.selectCustomer = function () {
var modalInstance = $modal.open({
templateUrl: paths.templates + 'customerSelectionModal.html',
controller: 'modalInstanceController',
resolve: {
customers: function () {
return $scope.customers;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.customerName = selectedItem.name;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
}]);
Finally the modalInstanceController.js controller for the modal form:
app.controller('modalInstanceController',
function ($scope, $modalInstance, customers) {
$scope.customers = customers;
$scope.selected = {
item: $scope.customers[0]
};
$scope.ok = function () {
alert($scope.selected.item.name);
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
When the modal dialog is displayed initially I get You have selected: {"item":{"name":"Terry Tibbs"}} displayed correctly as this is the default customer
selected by the modalInstanceController controller. However, when I select a customer I get You have selected: {"item":"{\"name\":\"Terry Tibbs\"}"} and clicking the OK button just displays undefined in the alert
window and I don't know why.
The only possible clue is when a customer is selected the name property and it's value are escaped using a \ for some odd reason that I haven't been able to figure out.
Has anyone any clue as to what's going on here?
Lastly, is it possible to set the radio button to the selected customer as it doesn't put a selection against the customer?
I am using the following:
AngularJS v1.3.9
Twitter Bootstrap v3.3.1
Angular UI Bootstrap v0.12.0
Anyway, in the radio button, try to use ng-value instead value attribute.