AngularJS and Laravel Blade: Module Error when changing the interpolateProvider (Delimiter) - angularjs

I have a strange problem when combining Laravel 5 (with Blade) and Angular 1.3.
I am experienced with Laravel, but a newbie with Angular. I know that I have to change Angular's delimiters to be able to make it work with Laravel's Blade.
So here is what I did:
//app.js
(function(){
var app = angular.module('TeamManager', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
app.controller('TeamController', function(){
// Do Something
});
})();
An in my View-File I definded ng-app and ng-controller. My goal is to iterate through a JSON. The JSON is not part of the JS above - I am aware of that.
<div class="container" ng-app="TeamManager">
<hr>
<div class="row" ng-controller="TeamController as team">
<div class="col-xs-4">
<div class="teamlist-container">
<table class="table table-striped">
<tr ng-repeat='member in teammembers'>
<td><% member.firstname %> <% member.lastname %></td>
</tr>
</table>
</div>
</div>
</div><!-- /row -->
<hr>
If I leave out the $interpolateProvider code, everything work and no errors are shown on the console. With it - however - nothings runs anymore. I get a Uncaught Error: [$injector:modulerr]
When I follow it, I come to: Error: $injector:unpr Unknown Provider
Am I missing something? I tried code from AngularJS Docs and some Tutorials. So it should be fine. Every time I was running in this error and it is driving me crazy.
I someone could help me with this, I would really appreciate it.
Many thanks,
AFX

I don't think your injecting the provider correctly, it should be in a callback for the config method of your app.
Try:
var app = angular.module('TeamManager', []);
app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});
Update: in the case of the OP, the minifying process wasn't jiving with the angular syntax being used. The asker would need to inject dependencies in the following way to use with minify/uglify:
var app = angular.module('TeamViewer', ['deps', function(deps) {
//...
}]);
And:
app.config(['$interpolateProvider', function($interpolateProvider) {
//...
}]);
This syntax is minify and uglify safe.

You don't have to change $interpolate. To use the default {{ }} delimiters for angular, simply add #{{}}, and blade will let javascript handle that for you.

Related

Expression not printing to page with AngularJS

I'm a AngularJS newbie. I have to learn it to help maintain an existing site. I made a simple angularjs app to just print hello but it is not working. What is wrong with my code. Thank you.
Codepen: https://codepen.io/centem/pen/pozRNqN?editors=1010
html
<div ng-app="testApp" ng-controller="testController">
{{test}}
</div>
angularjs code
var app = andgular.module('testApp', [])
app.controller('testController', function($scope) {
$scope.test = 'hello'
})
andgular in your js code is a typo. Just replace with angular.

My template engine is messing with Angular

Really stuck here so will appreciate the help.
Let me explain my set up/stack first:
Backend - Node with Express
Frontend - Angular
Snippet from the Node part of the code:
var router = express.Router();
router.get('/main', function(req, res){
res.render('wall', {siteTitle:'Home'});
});
Snippet from the HTML page:
<p>{{ siteTitle }}</p>
<div ng-controller="wallPosts">
<div ng-repeat="post in posts">
<p>{{ post.title }}</p>
</div>
<div>
Snippet from the Angular controller:
var angularApp = angular.module('angularApp', []);
angularApp.controller('wallPosts', ['$scope', '$http', function($scope, $http){
$http.get('/api/posts')
.success(function(result){
$scope.posts = result;
console.log(result);
})
.error(function(data, status){
console.log(data);
});
}]);
Okay so here is the problem - {{ post.title }} bit in the HTML page is not getting updated. However, this is: {{ siteTitle }}
The difference - post.title comes from the Angular app whereas the siteTitle comes from the res.render from Node.
Also, the console.log in the Angular controller works as I wanted to rule out there being a problem with the connection. Also, I have ruled out the '/api/posts' API call as it does indeed pass on an array of objects.
I have configured my Node to use 'hogan-express' as below:
Snippet from Node:
app.engine('html', require('hogan-express'));
So - can someone help me out here? What am I doing wrong? I want my Node to do the routing plus act as an API server and Angular just to get the data from the APIs and just chuck them out to the HTML page.
Thanks in advance,
Shayan
Use something like this {[]} as either your server-side template syntax or your client-side (Angular) template syntax. Shouldn't matter which one you set. If you choose client-side, set it within the Angular config injecting in the $interpolateProvider. See the Angular docs for more info.
Also I think this is a possible dup...
The issue is that template tag for both your AngularJS and NodeJS template engine are same.
One fix is to change the AngularJS template tag to some something
other than {{. You can do it by config.
Or change the template tag of the NodeJS template engine to
something other than {{
Or for a quick fix you can move following code to a separate html
and include it using ng-include.
<div ng-controller="wallPosts">
<div ng-repeat="post in posts">
<p>{{ post.title }}</p>
</div>
<div>

ng-bind-html and angular.js error

I´m playing with angular.js and try to learn ng-bind and ng-bind-html.
First, my code:
<div ng-app="module" ng-controller="controller as ctrl">
<div ng-bind-html="ctrl.html"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular-sanitize.js"></script>
<script>
var themodule = angular.module("module", ["ngSanitize"]);
themodule.controller("controller", [function()
{
this.html = "<strong>Hello</strong><div>More</div>";
}
]);
</script>
Even when it works, in the console appears the following ERROR:
Error: c.push is not a function
yc/this.$get</D.$$addBindingInfo<#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:73:319
$d</<.compile/<#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:214:329
$#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:70:197
B#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:59:255
g#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:51:335
B#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:59:199
g#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:51:335
D/<#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:50:444
uc/d/</<#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:18:4
Pe/this.$get</n.prototype.$eval#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:11
Pe/this.$get</n.prototype.$apply#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:126:236
uc/d/<#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:17:477
e#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:36:313
uc/d#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:17:1
uc#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:18:179
Jd#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:17:1
#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:250:429
a#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:164:283
lf/c#https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js:32:384
<div class="ng-binding" ng-bind-html="ctrl.html">
I searched but I could not find anything about it.
Does anyone know WHY and HOW to resolve it?
Thanks!
try to use this version of angular-sanitize.js
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-sanitize.js"
OR
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-sanitize.js"
and use
var app = angular.module("myFirstApp", ['ngSanitize']);/*"angular-sanitize.js",
which has functions for removing potentially dangerous tokens from the HTML.*/
I hope your problem will be resolved.

Angular - ng-bind-html not working due to ngSanitize error

Please see relevant jsFiddle
Within this file even though I included angular.sanitize.js I am not able to bind the html elements.
HTML Code:
<div ng-app="HelloApp">
<div ng-controller="MyCtrl">
<ul ng-repeat="snippet in myHTML" ng-bind-html="snippet"></ul>
</div>
</div>
JS Code:
var app = angular.module('HelloApp', ['ngSanitize'])
app.controller('MyCtrl', function($scope) {
$scope.myHTML = [];
$scope.myHTML.push('<li>Test1</li>');
$scope.myHTML.push('<li>Test2</li>');
});
Nothing is being displayed when running the jsFiddle. When debugging I am getting an injector error.
Please let me know if you need any more relevant information
Make sure ngSanitize is loaded after Angular. Updated working fiddle: http://jsfiddle.net/36qp9ekL/444/

getting angularjs to work in jsfiddle?

I'm trying to make an angularjs jfiddle for another question, but I can't get it working. Can somebody look at it and let me know what I'm doing wrong?
<div ng-app="myApp">
<div ng-conroller="MyController">
Click me: <input type="checkbox" ng-model="checked"/><br/>
<div>
{{checked}}
</div>
</div>
</div>
js:
var app = angular.module('myApp', [
'ngAnimate',
'my.controllers'
]);
var controllers = app.module('my.controllers', []);
controllers.controller('MyController', function($scope) {
$scope.checked = true;
});
fiddle link
fiddle link without external libraries
fiddle link with only ng-animate ext library
Can it be that it's because jsfiddle adds a "http://fiddle.jshell.net/_display/" in front of any external library location? Like when I try to add "ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.7/angular-animate.js" then jsfiddle changes it to "http://fiddle.jshell.net/_display/ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.7/angular-animate.js"
WHY?
You need to set option no wrap - in <body>
You should use:
var controllers = angular.module('my.controllers', []);
Instead of:
var controllers = app.module('my.controllers', []);
This fiddle works: http://jsfiddle.net/NBhn4/1/
EDIT:
To work with ng-animate you need to include external libraries in correct order and use No-Library (pure JS) option or eg. any jQuery library:
Updated fiddle: http://jsfiddle.net/NBhn4/175/
I am writing my answer for those who land on this page , I was used to use ng-module directive but in jsfiddle after half an hour I realized that ng-module is not allowed and you see no error , and when changed that ng-module to ng-app fiddle worked very well .I just wanted to share this .
<div ng-app="appX" ng-controller="appCtrl">
<p>{{greeting}}
</p>
</div>
var app=angular.module("appX",[]);
console.log(app);
app.controller("appCtrl",function($scope){
$scope.greeting="Hello World";
});
https://jsfiddle.net/furkankatman/trgrjwf1/7/

Resources