ng-include seems to do nothing - angularjs

http://jsfiddle.net/fresheyeball/5z7bX/
<script type="text/ng-template" id="/newTrack.html">
<h1>New Track </h1>
<p>Hello there</p>
</script>
<div id="overlay" closemodal></div>
<div id="modal"><a id="closeModal" closemodal></a>
<div id="modalContent" ng-include src="/newTrack.html"></div>
</div>
I would expect the following result:
<div id="overlay" closemodal></div>
<div id="modal"><a id="closeModal" closemodal></a>
<div id="modalContent" ng-include src="/newTrack.html">
<h1>New Track </h1>
<p>Hello there</p>
</div>
</div>
But nothing seems to happen. Really I am trying to render the template from inside a custom directive, after tracking it down for a while, it appears I can't get ng-include to working even in its most simple form. What am I missing here?

Under Fiddle Options, add <body ng-app>
src: If the source is a string constant, make sure you wrap it in quotes, e.g.,src="'myPartialTemplate.html'". -- ngInclude docs
So ng-include src="'/newTrack.html'"
Fiddle

Related

AngularJS - Display Banner after 2 rows, followed by another 2 Rows

I'm trying to display a Banner <div> after 2 rows of 4 x "col-md-3", followed by another 2 Rows - so the resulting markup would look like:
<div class="col-md-3">1</div>
<div class="col-md-3">2</div>
<div class="col-md-3">3</div>
<div class="col-md-3">4</div>
<div class="col-md-3">5</div>
<div class="col-md-3">6</div>
<div class="col-md-3">7</div>
<div class="col-md-3">8</div>
<div class="col-md-12" id="Banner">Banner</div>
<div class="col-md-3">9</div>
<div class="col-md-3">10</div>
<div class="col-md-3">11</div>
<div class="col-md-3">12</div>
<div class="col-md-3">13</div>
<div class="col-md-3">14</div>
<div class="col-md-3">15</div>
<div class="col-md-3">16</div>
Trying to simulate this using AngularJS so would have to use ng-repeat - but cannot seem to get this working. Any help appreciated: My Plunker
<div ng-repeat="n in Numbers">
<div class="col-md-3">{{n}}</div>
</div>
There are just a couple of problems with your plunker. First maincontroller.js is missing the .js extension, so it is never loaded.
Next all of the references to load angular should start with https instead of just http.
To display the banner after a certain number of rows place it within the first div and use something like:
<div class="col-md-12" id="Banner" ng-if="$index==5">Banner</div>
Not sure if 5 is the number you want there or if you want to use some kind of expression to see if ng-if is divisible by a certain value, but using $index and ng-if should get you there.
You could use ng-repeat-start and ng-include with a ng-if to conditionally include the banner if it's at the correct index with n===8.
Please have a look at the code below or in this plunkr.
angular.module("app", [])
.controller("MainController", mainController);
function mainController($scope) {
$scope.Numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container" ng-controller="MainController" ng-app="app">
<h1>Demo</h1>
<div class="col-md-3" ng-repeat-start="n in Numbers">{{n}}</div>
<div ng-include="'banner.html'" ng-if="(n === 8)" ng-repeat-end></div>
<script type="text/ng-template" id="banner.html">
<div class="col-md-12" id="Banner">Banner</div>
</script>
</div>

declare one controller for each tab

I'm using tabs in an html page and I'm trying to declare one controller for each tab.
<div class="tab-content">
<div class="tab-pane active" id="customerInfo">
#customerList()
</div>
<div class="tab-pane" id="communities">
#communityList()
</div>
For the first tab I declare my controller in "customerList" like this :
<div class="block full" ng-controller="customerCtrl">
and I tried the same thing with the second tab but it didn't work. Any help please.
This is possible and judging from here you can do this as simple as:
<body ng-app="">
<div class="tab-content">
<div class="tab-pane active" id="customerInfo" ng-controller="customerListcontroller">
#customerList()
</div>
<div class="tab-pane" id="communities" ng-controller="communityListcontroller">
#communityList()
</div>
</div>
</body>
There are few things you might be doing wrong:
forget to put ng-app="app" to wrap your controllers
From the question there is a div omission. Make sure this is not the case in the code
Not defining your controllers properly.
Not calling the file containing the controller in the HTML file
It should be a minor fix. You can also take a look at this.

Issue with angular-based codepen not loading my inline templates using ng-include

I am trying to load angular inline templates as follows:
<ng-include src="templateId"></ng-include>
Here is the inlined template:
<script type="text/ng-template" id="needs.html">
<div class="form-group">
<div class="col-lg-6">
<div ng-repeat="need in needs" class="hidden-radios">
<input type="radio" id="{{need}}" name="needs" ng-required="true" ng-model="advertisement.need" ng-value="need"/>
<label for="{{need}}" class="col-lg-6">
<span class="block-span">
{{ need }}
</span>
</label>
</div>
</div>
</div>
</script>
and the relevant portion from the controller:
$scope.focusNeed = function(){
console.log('focusNeed');
$scope.templateId='needs.html';
};
See codepen here: http://codepen.io/balteo/pen/ogBBXZ?editors=101
The issue I have is that the app tries to load the template by issuing request on the server instead of looking at the inline templates.
Can anyone please help?
That is because your template is out of scope. Place <script type="text/ng-template" id="needs.html"> inside ng-app and it will work.
In real world ng-app is on the body. Butin codepen you cannot do that. That is why you'd better use plunker.

How can I call an angular controller function from my custom polymer web component?

I've seen this question and seen it answered but not in any way I can use or understand. The question is very specific and simple but somehow the answers are a bit over my head.
All I want to do is have a very simple menu work. when i menu item is clicked, I need it to of course use my angular controller function to handle it. I want all of that on the angular side. It actually does work when I simply use the elements directly without declaratively making my own.
So this works just fine and my ng-click is successfuly calling my acceptedSelect() in my controller:
<core-menu>
<div ng-repeat="order in acceptedOrders" ng-click="acceptedSelect(order)">
<paper-item class="my-paper-item" >
<div class="order-list-item">
<div class="order-number">{{order.Order}}</div>
<div class="order-date"> {{order.Date}} </div>
<div style="clear: both"></div>
<div class="order-address">
{{order.Property}}
</div>
</div>
</paper-item>
</div>
</core-menu>
But i do the same thing by wrapping the paper element in a polymer-element, it doesn't work. So this will not work:
<polymer-element name="my-app" attributes="refresh">
<template>
<style>
...
</style>
<core-menu>
<div ng-repeat="order in acceptedOrders" ng-click="acceptedSelect(order)">
<paper-item class="my-paper-item" >
<div class="order-list-item">
<div class="order-number">{{order.Order}}</div>
<div class="order-date"> {{order.Date}} </div>
<div style="clear: both"></div>
<div class="order-address">
{{order.Property}}
</div>
</div>
</paper-item>
</div>
</core-menu>
</template>
<script>
Polymer('my-app', {
});
<my-app></my-app>
I'm just not sure what I can do to make this work. Any help is greatly appreciated.

Is it possible to add template trough angular.js from outside of ng-view?

I want to have a login view outside ng-view, but is it even possible with angular.js? couldnt find any examples of folowing on the internet. Example is descibed below.
<div class="container">
<div class="header">
<div class="loginView"> my huge login view</div>
</div>
</div>
<div class="container">
<div ng-view></div>
</div>
Yes. Assign a controller to the loginView and treat it like any other view.
ng-view is just used when using the $routeProvider to define routes.
This is perfectly valid. ngView is used to complement the router. This means it is just a directive as any other. You can put anything around it.
It sounds like you want something like this: Live demo here (click).
<div class="container">
<div class="header">
<div class="loginView" ng-include="'login.html'"></div>
</div>
</div>
You could also include your file from a $scope property like this:
$scope.foo = 'login.html';
<div ng-include="foo"></div>

Resources