Adding angular code in script section is not working in jsfiddle - angularjs

Add the following code in html box and it will work as it should be
<div ng-app="myapp" data-ng-controller='Ctrl'>
<h1> Header </h1>
<h3> {{test}} </h3>
</div>
<script>
var app = angular.module('myapp', []);
app.controller('Ctrl', function($scope){
$scope.test = "Hello";
});
</script>
Now move the JavaScript to JavaScript box
var app = angular.module('myapp', []);
app.controller('Ctrl', function($scope){
$scope.test = "Hello";
});
It start giving error: Uncaught Error: No module: myapp
I don't want to write everything in html section, any fix?

While I was playing with setting, found the answer :)
Change the setting Load Type from "OnLoad" to "No wrap - in ", see the attached image.
Strange but it is working like charm :)

Related

VM73:49Uncaught ReferenceError: angular is not defined in jsfiddle

I am trying to run an angularJS example in jsfiddle (ofcourse learning purpose) getting below errors in console.
When I add this cdn for angular compatibility:
https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js
I seen below error.
VM141 angular.min.js:6 Uncaught Error: [$injector:modulerr]
http://errors.angularjs.org/1.5.6/$injector/modulerr?p0=myApp&p1=Error%3A%2…oudflare.com%2Fajax%2Flibs%2Fangular.js%2F1.5.6%2Fangular.min.js%3A21%3A19)
When I remove the external source, seen below error:
VM93:45 Uncaught ReferenceError: angular is not defined
Concern:
Do I need to add external source for angular example? If yes then what will be the correct way to add external resource. (like, search a cdn for that api and add).
Here's the code snippet:
html:
<div ng-app="myApp" ng-controller="myCtrl">
Your name: <input type="text" ng-model="name" ng-click="displayName();">
Hello {{$scope.name}}
</div>
js:
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope){
$scope.displayName = function(){
alert("In right place");
$scope.name = "Name";
}
});
Here's the fiddle: https://jsfiddle.net/supdas87/my1juu4e/2/
I've added angular-1.0.1.js file to external resources (you just have to copy and paste the URL), and changed {{$scope.name}} by {{name}} on your HTML file. You do not have to write $scope when you use {{ }}, it is implicit.
Here is a working Fiddle based on the code you provided.
PS: I've added Angular 1.0.1, but of course you can use the version of Angular you want. You can see allreleases here.
HTML
<div ng-app="myApp" ng-controller="myCtrl">
Your name: <input type="text" ng-model="name" ng-click="displayName();">
Hello {{name}}
</div>
JavaScript
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope) {
$scope.displayName = function(){
alert("In right place");
$scope.name = "Name";
}
});

How to write an efficient code div.innerHTML like code in AngularJS

If i need to change a small html dynamically on or before a success or error callback of ajax in html I do
document.getElementById("xyz").innerHTML("<img />");
in jQuery also i can do
jQuery("#xyz").html("<img />");
now I can also use handlebars for doing more complex html and attaching it in the above code.
So how can I do the above such things in Angular.js
Please give some examples of some helpful pointers
using ng-bind-html like this,
html:
<div ng-app="myApp" ng-controller="myCtrl">
<p ng-bind-html="myText"></p>
</div>
js:
var app = angular.module("myApp", ['ngSanitize']);
app.controller("myCtrl", function($scope) {
$scope.myText = "<img />";
});

Angularjs href attribute remove if value is mailto:email

I need to show link in page having attribute value mailto.
normal href attribute value working fine but if value is email than it removed
Code:
myCtrl.link = '<code>Email</code>';
<code><span ng-bind-html="myCtrl.link"></span></code>
rendered output:
<code><a target="_blank">Email</a></code>
Please suggest how to handle anchor having href value like mailto:sulok#atlogys.com
You are running into a "security" issue,
please have a look at this doc-page...
Just say "this is safe" to angular:
function TestCtrl(vm, $sce) {
'use strict';
var htmlString = '<code>Email</code>';
vm.link = $sce.trustAsHtml(htmlString);
}
angular
.module('test', [])
.controller('TestCtrl', ['$scope', '$sce', TestCtrl])
;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<article ng-app="test">
<div ng-controller="TestCtrl">
<code><span ng-bind="link"></span></code>
</div>
</article>

Angular JS: Controller not working

I am doing a simple example but for some reason I am unable to print the value in message variable that I pass through $scope in my controller.
please find my code as give in this fiddle: http://jsfiddle.net/yy3vuzfk/1/
html page code:
<div ng-app>
<div>Hello AJS</div>
<div> Sum of 2 and 3 is: {{ 2 + 3}}
<div ng-controller="myController">
Message: {{ message }}
</div>
</div>
script.js code
var myController = function($scope){
$scope.message = "My Message";
};
My output html
Hello AJS
Sum of 2 and 3 is: 5
Message: {{ message }}
any help is appreciated
You first need to initalize angular by creating a module and then creating a controller in that module.
You can see this updated fiddle.
angular.module('myApp', [])
.controller("MyController", function($scope) {
$scope.message = "My Message Is Super Awesome";
});
You can find basic examples # Angularjs.org home page. Look at the "Add Some Control" section on that page.

ng-keypress not triggering function

I'm new to AngularJS. I'm implementing ng-keypress events in AngularJS.
I referred to many blogs and tried to do as it is shown, but my code is not working!
<div ng-app="myApp">
<div ng-controller="MainCtrl">
<input ng-keypress="change($event)" type="text" >
{{ text }}
</div>
</div>
script is:
var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', ['$scope', function ($scope) {
$scope.change=function($event){
$scope.text= 'a';
};
}]);
I'm trying to change the value of {{text}} on keypress.. but it's not working!
can anyone help me out!
Thanks :)
i tried the same things and its working
<body ng-controller="MainCtrl">
<input ng-keypress="change($event)" type="text" >
<pre>{{name}}</pre>
</body>
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.change=function($event){
$scope.name= 'hello';
};
});
checkout this plunker... PLUNKER LINK
EDIT
checkout your code plunker.. thats also working
EDIT
finally the answer is: 1.0.7 does not support ng-keypress, hence it was not working..
sometimes ng-keypress do not work on some browsers !!
i had the issue with the arrow keys in chrome
try ng-keydown instead (it worked for me )
As everyone else says it's working as it is supposed to do. Perhaps you want something like this?
var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', ['$scope', function ($scope) {
$scope.text = '';
$scope.change=function($event){
$scope.text += String.fromCharCode($event.keyCode);
};
}]);

Resources