Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple AngularJS running on JSFiddle

How do I make a jsfiddle out of the following code:

<html>
<head>
</head>
<body>
    <div ng-app ng-controller="MainCtrl">
        <ul>
            <li ng-repeat="num in nums">
                {{num}}
            </li>
        </ul>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.js"></script>

    <script type="text/javascript" charset="utf-8">
        function MainCtrl($scope) {
            $scope.nums = ["1","2"];
        }
    </script>
</body>
</html>

My non working attempt: http://jsfiddle.net/zhon/3DHjg/ shows nothing and has errors.

like image 822
zhon Avatar asked Feb 06 '13 16:02

zhon


People also ask

How do I run JSFiddle code?

Entering and running code JSFiddle has the notion of panels (or tabs if you switch into the tabbed layout), there are 4 panels, 3 where you can enter code, and 1 to see the result. Once you enter code, just hit Run in the top actions bar, and the fourth panel with results will appear.

Does JSFiddle run locally?

jsfiddle's js is all local too. Local to browser.

Is JSFiddle an IDE?

JSFiddle is an online IDE which is designed to allow users to edit and run HTML, JavaScript, and CSS code on a single page.


4 Answers

You need to set some things up in jsFiddle for this to work.

First, on the left panel, under "Frameworks & Extensions", select "No wrap - in <body>".

Now, under "Fiddle Options", change "Body tag" to <body ng-app='myApp'>

In the JS panel, initiate your module:

var app = angular.module('myApp', []);

Check it out: http://jsfiddle.net/VSph2/1/

like image 100
dutzi Avatar answered Nov 15 '22 20:11

dutzi


@pkozlowski.opensource has a nice blog post about how to use jsFiddle to write AngularJS sample programs.

like image 31
Mark Rajcok Avatar answered Nov 15 '22 20:11

Mark Rajcok


You've defined your controller in a function scope that is not accessible to angular (angular is not yet loaded). In other words you are trying to call angular library's functions and helpers like below example before getting angular library loaded.

function onload(){
 function MainCtrl(){}
}

To resolve this, switch your angular load type to be No wrap - in <body> like shown in screenshot.

enter image description here

here is a working example in jsfiddle

like image 41
mpm Avatar answered Nov 15 '22 20:11

mpm


Click JAVASCRIPT button, choose angular version and place where u want include loaded script: enter image description here

Then click HTML button and add ng-app in body tag. Its all:) enter image description here

like image 31
Vasyl Gutnyk Avatar answered Nov 15 '22 20:11

Vasyl Gutnyk