admin管理员组

文章数量:1330564

This probably a bo jsFiddle/Angular question, but I'm trying to learn me some basic Angular and I'm not sure why it only works when I include the controller JS in the HTML pane. jsFiddle here

Basically the following works:

<div ng-app="myAppModule" ng-controller="someController">
<!-- Show the name in the browser -->
 <h1>Wele {{ name }}</h1>

<p>made by : {{userName}}</p>
<!-- Bind the input to the name -->
<input ng-model="name" name="name" placeholder="Enter your name" />
</div>
<script>
var myApp = angular.module('myAppModule', []);
myApp.controller('someController', function($scope) {
    // do some stuff here
    $scope.userName = "skube";
});
</script>

But if I try to move the JS within the script tag to the JavaScript pane, it fails.

This probably a bo jsFiddle/Angular question, but I'm trying to learn me some basic Angular and I'm not sure why it only works when I include the controller JS in the HTML pane. jsFiddle here

Basically the following works:

<div ng-app="myAppModule" ng-controller="someController">
<!-- Show the name in the browser -->
 <h1>Wele {{ name }}</h1>

<p>made by : {{userName}}</p>
<!-- Bind the input to the name -->
<input ng-model="name" name="name" placeholder="Enter your name" />
</div>
<script>
var myApp = angular.module('myAppModule', []);
myApp.controller('someController', function($scope) {
    // do some stuff here
    $scope.userName = "skube";
});
</script>

But if I try to move the JS within the script tag to the JavaScript pane, it fails.

Share Improve this question asked Jan 28, 2014 at 21:57 skubeskube 6,1859 gold badges57 silver badges82 bronze badges 3
  • 3 No wrap - in <head> jsfiddle/E9bU5/3 – elclanrs Commented Jan 28, 2014 at 21:58
  • I swear I tried that, but you're correct. What does "No wrap - in <head>" mean? It doesn't make sense to me – skube Commented Jan 28, 2014 at 22:00
  • 2 jsFiddle wraps your code in a ready or load event by default, and Angular might not find what it needs in the global scope when piling your template at the time is needed. No wrap in <head> just loads the script in the head, without a wrap. – elclanrs Commented Jan 28, 2014 at 22:04
Add a ment  | 

1 Answer 1

Reset to default 5

Take a look at this article for some info about jsFiddle with Angular

Also more examples on AngularJS Wiki that you could use: https://github./angular/angular.js/wiki/JsFiddle-Examples

本文标签: javascriptSimple AngularJS example with a module and controller in jsFiddleStack Overflow