angularjs controller not binding to partial view - angularjs

One of the two controllers (AboutCtrl) defined is working as expected. The default controller, TransactionCtrl is paired with the correct partial view but there isn't any angularjs magic happening. For example, I have a simple expression 1 + 2 = {{ 1+2}} that is not resolving (displays as written).
Here's the app definition:
angular.module('pennyWatch', ['ui.bootstrap'])
.config(['$routeProvider', function ($routeProvider) {
return $routeProvider
.when('/', { title: 'Transactions', templateUrl: 'partials/transaction.view.html', controller: TransactionCtrl })
.when('/about', { title: 'About', templateUrl: 'partials/about.view.html', controller: AboutCtrl })
.when('/transaction', { title: 'Transactions', templateUrl: 'partials/transaction.view.html', controller: TransactionCtrl })
.otherwise({ redirectTo: '/' });
}])
Here's the controllers. I can hit a breakpoint in AboutCtrl but not in the other one. I added TransactionCtrl.$inject recently to see if that would help but there isn't a difference:
function TransactionCtrl($scope, $location, logger, ngGrid) {
logger.log("in TransactionCtrl");
$scope.transactionList =
[
{ TransactionID: 1, Title: "TuElectric", Category: "Expense", Amount: "$ 76.80", Date: "Jan 1" },
{ TransactionID: 2, Title: "QT", Category: "Expense", Amount: "$ 62.38", Date: "Jan 1" },
{ TransactionID: 3, Title: "Kroger", Category: "Expense", Amount: "$ 123.67", Date: "Jan 3" },
{ TransactionID: 4, Title: "All State", Category: "Expense", Amount: "$ 248.17", Date: "Jan 3" },
{ TransactionID: 5, Title: "Credit Union", Category: "Income", Amount: "$1,897.00", Date: "Jan 4" }
];
$scope.error = "";
$scope.gridOptions = {
data: "transactionList"
//, showGroupPanel: true
};
}
TransactionCtrl.$inject = ['$scope', '$location', 'logger', 'ngGrid'];
function AboutCtrl($scope, logger) {
$scope.logEntries = logger.logEntries;
}
The partial view:
<!-- Transaction page partial view -->
<div style="position: absolute; top: 20px; left:2em;">
About
</div>
<p class="error" data-ng-show="error.length">{{error}}</p>
<br/>
<article>
<header>
<h2>Transaction List</h2>
</header>
<p>(trnx view) 1 + 2 = {{ 1 + 2 }}</p>
<ul id="fauxTable">
<li>
<p><span>Title</span><span>Category</span><span>Amount</span><span>Date</span></p>
</li>
<li data-ng-repeat="item in transactionList">
<p><span>{{item.Title}}</span><span>{{item.Category}}</span><span>{{item.Amount}}</span><span>{{item.Date}}</span></p>
</li>
</ul>
</article>
<article>
<h2>Transaction List (ngGrid)</h2>
<div class="gridStyle" ng-grid="gridOptions">
</div>
</article>
Thanks!

Related

How can I disable other tabs after clicking a particular tab?

I am trying to disable all other tabs when clicking on a particular tab. Means, suppose, initially when the application runs, tab1 appears as default. After that if I select tab2, all other tab should gets disabled and only gets enable back, when we click on Cancel button.
HTML code:
<tabset>
<tab
ng-repeat="t in tabs"
heading="{{t.heading}}"
select="go(t.route)"
active="t.active">
</tab>
</tabset>
<div ui-view></div>
JS code:
var app = angular.module("routedTabs", ["ui.router", "ui.bootstrap"]);
app.config(function($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise("/main/tab1");
$stateProvider
.state("main", { abtract: true, url:"/main", templateUrl:"main.html" })
.state("main.tab1", { url: "/tab1", templateUrl: "tab1.html" })
.state("main.tab2", { url: "/tab2", templateUrl: "tab2.html" })
.state("main.tab3", { url: "/tab3", templateUrl: "tab3.html" })
.state("main.tab4", { url: "/tab4", templateUrl: "tab4.html" });
});
$scope.go = function(route){
$state.go(route);
};
$scope.active = function(route){
return $state.is(route);
};
$scope.tabs = [
{ heading: "Tab1", route:"main.tab1", active:false },
{ heading: "Tab2", route:"main.tab2", active:false },
{ heading: "Tab3", route:"main.tab3", active:false },
{ heading: "Tab4", route:"main.tab4", active:false },
];
$scope.$on("$stateChangeSuccess", function() {
$scope.tabs.forEach(function(tab) {
tab.active = $scope.active(tab.route);
});
});
Please find the code snippet below, Additionally added onSelectTab(). Let me know if you need any changes.
DEMO
HtmlFile
<div ng-controller="mainController">
<tabset>
<tab
ng-repeat="t in tabs"
heading="{{t.heading}}"
select="go(t.route, true)"
ng-click="onSelectTab(t)"
active="t.active"
disabled="flags.disableTab && !t.active">
</tab>
</tabset>
<button class="btn" ng-click="flags.disableTab = false">CANCEL</button>
<div ui-view></div>
</div>
controllerFile.js
app.controller("mainController", function($rootScope, $scope, $state, $timeout, $filter) {
$scope.flags = {};
$scope.go = function(route) {
$state.go(route);
};
//Additionally added below method
$scope.onSelectTab = function(t) {
if (t.heading == 'tab2') {
$scope.flags.disableTab = true;
}
};
//End
$scope.active = function(route) {
return $state.is(route);
};
$scope.tabs = [{
heading: "tab1",
route: "main.tab1",
active: false
}, {
heading: "tab2",
route: "main.tab2",
active: false
}, {
heading: "tab3",
route: "main.tab3",
active: false
}, {
heading: "tab4",
route: "main.tab4",
active: false
}, ];
$scope.$on("$stateChangeSuccess", function() {
$scope.tabs.forEach(function(tab) {
tab.active = $scope.active(tab.route);
});
});
});
Note: Accept the answer if it is works

Pass the id of listview on click to next page in ionic 1

app.js
var app = angular.module('starter', ['ionic'])
app.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
app.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('first', {
url: '/1',
templateUrl: 'templates/first.html',
controller:'firstCtrl'
})
.state('next', {
url: '/2',
templateUrl: 'templates/next.html',
controller:'nextCtrl'
})
$urlRouterProvider.otherwise("/1");
})
app.controller('firstCtrl', function($scope) {
$scope.items = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
{ id: 6 },
{ id: 7 },
{ id: 8 },
{ id: 9 },
{ id: 10 },
{ id: 11 },
{ id: 12 },
{ id: 13 },
{ id: 14 },
{ id: 15 },
{ id: 16 },
{ id: 17 },
{ id: 18 },
{ id: 19 },
{ id: 20 }
]
$scope.selectedElem = function(item){
$scope.itemid=item.id;
}
})
app.controller('nextCtrl', function($scope) {
$scope.input ='';
});
In first.html
<ion-view title="Ionic List View">
<ion-content>
<ion-item ng-repeat="item in items" ui-sref="next" ng-model="item" ng-
click="selectedElem(item.id)">
Item {{item.id}}
</ion-item>
</ion-content>
</ion-view>
In next.html
<ion-view title="Selected List View">
<ion-content>
<ion-item>
Item{{ itemId }}
</ion-item>
</ion-content>
</ion-view>
What you can do is use query params to set the next view. You'd end up with something like the following:
State
.state('item', {
url: '/item/:itemId',
templateUrl: 'templates/item-detail.html',
controller:'ItemCtrl'
})
Item List View
<ion-view title="Ionic List View">
<ion-content>
<ion-item ng-repeat="item in items" ng-model="item" ui-sref="item({itemId: item.id})">
Item {{item.id}}
</ion-item>
</ion-content>
</ion-view>
Controller
app.controller('ItemCtrl', function($scope, $stateParams) {
$scope.itemId = $stateParams.itemId;
});
Item Detail View
<ion-view title="Selected List View">
<ion-content>
<ion-item>
Item{{ itemId }}
</ion-item>
</ion-content>
</ion-view>
What I've done is:
Redefined your state to take an itemId url param
Redefined your ng-repeat to generate to proper url to the new item state with the correct itemId
Redefined your controller to take a dependency on $stateParams which has the values of all params passed to the state.
Set the item template to read the id from $scope

ng-click does not call function in mdDialog

I am a little new to AngularJS but I cannot figure out why the ng-click here will not call th addingSt() function, I wonder if it has something to do with the fact that it is being called from a mdDialog. Thanks for your help.
Heres my html for the mdDialog:
<md-dialog aria-label="Send Email">
<md-dialog-content>
<h3>Issue Details</h3>
<h4>Description</h4>
<md-input-container>
<label>Add description:</label>
<textarea class="form-control input-lg" style="width: 500px; height:100px;"></textarea>
</md-input-container>
<h3>Sub-tasks:</h3>
<md-list-item ng-repeat=" subtask in subtasks">
<p>{{subtask.content}}</p>
<md-checkbox aria-label="blarg" class="md-secondary" style="padding-right:60px;" ng-click="removeSubTask(subtask,$index)"></md-checkbox>
<md-list-item ng-if="addingTask === true"> <input ng-if="addingTask===true" ng-model="task.content" aria-label="blarg" placeholder="Add Subtask Here"></input>
</md-dialog-content>
<md-dialog-actions>
<md-button ng-show="addingTask === false" ng-click="addingSt()" class="btn btn-primary">
Add Sub-Task
</md-button>
<md-button ng-show="addingTask === true" ng-click="addingSt()" class="btn btn-primary">
cancel
</md-button>
<md-button ng-show="addingTask === true" ng-click="addSubTask()" class="btn btn-primary">
Submit
</md-button>
<md-button ng-click="closeDialog()" class="btn btn-primary">
Close
</md-button>
Here's the controller for the parent of the above mdDialog, (the controller for the mdDialog is nested inside it and works fine for all functions accept the addingSt() function)
var app = angular.module('epr')
app.controller('adminMainCtr',[ '$scope','$mdDialog',function($scope, $mdDialog) {
$scope.issues = [
{ name: 'Blizzard', img: 'img/100-0.jpeg', WardMessage: true, index:0, subtasks:[{content:"Shovel Sister Pensioner's Driveway "},
{content:"Clear downed trees at the Bush's home "}]},
{ name: 'Tornado', img: 'img/100-1.jpeg', WardMessage: false, index:1, subtasks:[{content:"",index:0}] },
{ name: 'Peterson Family Car Crash', img: 'img/100-2.jpeg', WardMessage: false, index:2, subtasks:[{content:"",index:0}] },
{ name: 'Flood', img: 'img/100-2.jpeg', WardMessage: false, index:3, subtasks:[{content:"",index:0}] },
{ name: 'School Shooting', img: 'img/100-2.jpeg', WardMessage: false, index:4, subtasks:[{content:"",index:0}] }
];
$scope.goToIssue = function(issue, event) {
var parentEl = angular.element(document.body);
$mdDialog.show({
//parent: parentEl,
templateUrl:'views/issue.html',
locals: {
items: $scope.items,
issue: issue
},
controller: DialogController
});
function DialogController($scope, $mdDialog) {
$scope.subtasks = issue.subtasks;
$scope.addingTask = false;
$scope.task={content:""};
$scope.closeDialog = function() {
console.log($scope.addingTask);
$mdDialog.hide();
}
$scope.removeSubTask = function(subtask,index){
$scope.subtasks.splice(index,1);
}
}
$scope.addSubTask = function() {
console.log("here");
}
$scope.addingSt = function() {
if($scope.addingTask === false) {
console.log($scope.addingTask);
$scope.addingTask = true;
return;
}
if($scope.addingTask === true) {
$scope.addingTask = false;
return;
}
}
}
}]);
Any help that you can lend me would be very appreciated!!!
You messed with the HTML and angylar code.
Errors found:
1) angular module initialization.
var app = angular.module('MyApp', ['ngMaterial'])
2) You placed some function outside the DialogController
3) md-list-item HTML has no end tags.
Created working Plunkr here. https://plnkr.co/edit/Sl1WzLMCd8sW34Agj6g0?p=preview . Hope it will solve your problem.
(function() {
'use strict';
var app = angular.module('MyApp', ['ngMaterial'])
app.controller('adminMainCtr', ['$scope', '$mdDialog', function($scope, $mdDialog) {
$scope.issues = [{
name: 'Blizzard',
img: 'img/100-0.jpeg',
WardMessage: true,
index: 0,
subtasks: [{
content: "Shovel Sister Pensioner's Driveway "
}, {
content: "Clear downed trees at the Bush's home "
}]
}, {
name: 'Tornado',
img: 'img/100-1.jpeg',
WardMessage: false,
index: 1,
subtasks: [{
content: "",
index: 0
}]
}, {
name: 'Peterson Family Car Crash',
img: 'img/100-2.jpeg',
WardMessage: false,
index: 2,
subtasks: [{
content: "",
index: 0
}]
}, {
name: 'Flood',
img: 'img/100-2.jpeg',
WardMessage: false,
index: 3,
subtasks: [{
content: "",
index: 0
}]
}, {
name: 'School Shooting',
img: 'img/100-2.jpeg',
WardMessage: false,
index: 4,
subtasks: [{
content: "",
index: 0
}]
}];
$scope.goToIssue = function(issue, event) {
var parentEl = angular.element(document.body);
$mdDialog.show({
templateUrl: 'mddialog.html',
locals: {
message: {
items: $scope.items,
issue: issue
}
},
controller: DialogController
});
}
function DialogController($scope, $mdDialog, message) {
console.log(message)
//$scope.subtasks = message.issue.subtasks;
$scope.addingTask = false;
$scope.task = {
content: ""
};
$scope.closeDialog = function() {
console.log($scope.addingTask);
$mdDialog.hide();
}
$scope.removeSubTask = function(subtask, index) {
$scope.subtasks.splice(index, 1);
}
$scope.addSubTask = function() {
console.log("here");
}
$scope.addingSt = function() {
if ($scope.addingTask === false) {
console.log($scope.addingTask);
$scope.addingTask = true;
return;
}
if ($scope.addingTask === true) {
$scope.addingTask = false;
return;
}
}
}
}]);
})();

How to bring dynamicaly scope in view

In my controller I have this code:
$scope.lists = [{
listName: 'list1'
}, {
listName: 'list2'
}];
angular.forEach($scope.lists, function(item) {
var listName = item.listName;
$scope[listName] = [{
Name: 'Stefan'
}, {
Name: 'Stefan'
}, {
Name: 'Stefan'
}, {
Name: 'Stefan'
}];
});
The Input from lists cames from a webservice, so the values (list1 and list2) can be different each time i reload the app. I can also more then 2 items in lists.
How can I show the value from $scope[listName] in an ng-repat section in my view?
Thanks for your Help.
Stefan.
You might try something like this:
(function() {
angular.module("myApp", []).controller("controller", ["$scope",
function($scope) {
$scope.lists = [{
listName: "list1"
}, {
listName: "list2"
}];
angular.forEach($scope.lists, function(item) {
var listName = item.listName;
$scope[listName] = [{
Name: "Stefan"
}, {
Name: "Stefan"
}, {
Name: "Stefan"
}, {
Name: "Stefan"
}];
$scope.results = $scope[listName];
});
}
]);
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div data-ng-app="myApp">
<div data-ng-controller="controller">
<ul>
<li data-ng-repeat="item in results">{{item.Name}}</li>
</ul>
</div>
</div>

How do you show ng-repeated children of filtered array?

I have a drop-down filtered array, but the array I'd like to use is a little more complex, with nested data, similar to this http://jsfiddle.net/vojtajina/u75us/
I'd like to combine both ideas, but can't figure out why my fiddle doesn't display the 'child nodes'
<div class="col-md-12" ng-controller="App04Ctrl">
<p>Search:
Filter:
<select ng-model="filterItem.store" ng-options="item.name for item in filterOptions.stores">
</select>
Sort:
<select ng-model="sortItem.store" ng-options="item.name for item in sortOptions.stores">
</select>
</p>
<ul>
<li ng-repeat="item in locations | orderBy:'price':reverse | filter:customFilter" >Name: {{item.name}} Price: {{item.price}} Location: {{item.location}}</li>
<ul>
<li ng-repeat="package in location.packages">{{package.name}} has services:
<ul>
<li ng-repeat="service in package.services">{{service.name}}</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
angular.js:
var app = angular.module('app04', []);
function App04Ctrl($scope) {
//Contains the filter options
$scope.filterOptions = {
stores: [
{id : 2, name : 'Show All', location: 'All Locations' },
{id : 3, name : 'Ashburn', location: 'Ashburn' },
{id : 4, name : 'San Francisco', location: 'San Francisco' },
{id : 5, name : 'Denver', location: 'Denver' },
{id : 6, name : 'Chicago', location: 'Chicago' },
{id : 7, name : 'Irvine', location: 'Irvine' }
]
};
//Contains the sorting options
$scope.sortOptions = {
stores: [
{id : 1, name : 'Price Highest to Lowest' },
{id : 2, name : 'Price Lowest to Highest' },
]
};
//Mapped to the model to filter
$scope.filterItem = {
store: $scope.filterOptions.stores[0]
}
//Mapped to the model to sort
$scope.sortItem = {
store: $scope.sortOptions.stores[0]
};
//Watch the sorting model - when it changes, change the
//ordering of the sort (descending / ascending)
$scope.$watch('sortItem', function () {
console.log($scope.sortItem);
if ($scope.sortItem.store.id === 1) {
$scope.reverse = true;
} else {
$scope.reverse = false;
}
}, true);
//Custom filter - filter based on the location selected
$scope.customFilter = function (locations) {
if (locations.location === $scope.filterItem.store.location) {
return true;
} else if ($scope.filterItem.store.location === 'All Locations') {
return true;
} else {
return false;
}
};
// Location data
$scope.locations = [{
name: "product1",
price: 198,
location: 'Ashburn',
packages: [{
name: 'Doom Patrol',
services: [{
name: 'Mento'}, {
name: 'Vox'}, {
name: 'Robotman'}]}, {
name: 'Suicide Squad',
services: [{
name: 'King Shark'}]}, {
name: 'Shadowpact',
services: [{
name: 'Zauriel'}, {
name: 'Enchantress'}, {
name: 'Ragman'}, {
name: 'Nightshade'}]}]}, {
name: "product2",
price: 402,
location: 'Chicago',
packages: [{
name: 'Metal Men'}, {
name: 'Legion of Superheroes',
services: [{
name: 'Ultra Boy'}, {
name: 'Kid Quantum'}]}]}, {
name: "product2",
price: 300,
location: 'Denver',
packages: [{
name: 'Freedom Fighters',
services: [{
name: 'Damage'}, {
name: 'Iron Munro'}]}, {
name: 'Birds of Prey',
services: [{
name: 'Huntress'}, {
name: 'Black Alice'}]}]}, {
name: "product2",
price: 1243,
location: 'Irvine',
packages: [{
name: 'The Outsiders'}, {
name: 'Zoo Crew',
services: [{
name: 'Rubberduck'}, {
name: 'Captain Carrot'}]}, {
name: 'The Elite',
services: [{
name: 'Vera Black'}, {
name: 'Manchester Black'}]}, {
name: 'Justice Legion Alpha'}]}
];
}
http://jsfiddle.net/jdacio/Vfx3y/2/
What am I missing? Am I on the right track? is there a better way to do this?
There are two problems that I see in your code above:
[1] Notice that you have closed the <li> tag, thus stopping the nesting of your ng-repeat directive to show the packages and and services of each item location. Simply remove the </li> closing tag and that should solve your first problem.
<li ng-repeat="item in locations | orderBy:'price':reverse | filter:customFilter" >
Name: {{item.name}}
Price: {{item.price}}
Location: {{item.location}}
</li> <!-- THIS IS THE PROBLEM!! -->
[2] As what Mosho mentioned, your nested ng-repeat directive is using a location reference which does not exist in the current context of its parent ng-repeat directive. The simplest solution would be to change
<li ng-repeat="package in location.packages">
to
<li ng-repeat="package in item.packages">
The resulting HTML code should be:
<ul>
<li ng-repeat="item in locations | orderBy:'price':reverse | filter:customFilter" >
Name: {{item.name}}
Price: {{item.price}}
Location: {{item.location}}
<ul>
<li ng-repeat="package in location.packages">{{package.name}} has services:
<ul>
<li ng-repeat="service in package.services">{{service.name}}</li>
</ul>
</li>
</ul>
</li>
</ul>
Instead of location.packages, it should be item.packages. (or 'location in locations' rather than 'item in locations').
<li ng-repeat="item in locations | orderBy:'price':reverse | filter:customFilter">
...
<li ng-repeat="package in location.packages">
You refer to location, but you declare item in locations.

Resources