How to load ace editor with requirejs from CDN? - javascript

I am trying to load the ace editor from a CDN with requirejs.
Here is a plunkr which illustrates my problem. Ace is not defined in the following case:
requirejs.config({
paths: { ace: ['//cdnjs.cloudflare.com/ajax/libs/ace/1.1.9/ace'] }
})
$('h1').text("loading ace...");
requirejs([ 'ace'], function(ace) {
$('h1').text("ace loaded.")
console.log(ace)
ace.edit('#editor')
return
})

you need to define ace as a path to the folder that contains ace.js
and require "ace/ace"
requirejs.config({
paths: { ace: ['//cdnjs.cloudflare.com/ajax/libs/ace/1.1.9/'] }
})
$('h1').text("loading ace...");
requirejs(['ace/ace'], function(ace) {
$('h1').text("ace loaded.")
console.log(ace)
ace.edit('editor')
})
<!DOCTYPE html>
<html>
<head>
<script src="http://requirejs.org/docs/release/2.1.14/minified/require.js"></script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<h1>Hello Plunker!</h1>
<div id='editor' style="height:200px"></div>
</body>
</html>

Related

Why simple javascript import is not working?

Here not working
https://plnkr.co/edit/a7H1ilzkUA3918vc?open=lib%2Fscript.js&deferRun=1
But here in similar example - works, from which I am trying to do:
https://plnkr.co/edit/XLSL581pjgwe3PI1?open=lib%2Fscript.js&deferRun=1
page1.js
export function myAlert() {
alert("car");
}
script.js
import { myAlert } from "./page1.js";
window.onload = myAlert;
index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="lib/style.css" />
<script src="lib/script.js"></script>
</head>
<!-- <body ng-app="plunker" ng-cloak>
<div ng-controller="MainCtrl">
<h1>Hello {{name}}</h1>
<p>Start editing and see your changes reflected here!</p>
</div>
</body> -->
</html>
package.json
{
"name": "#plnkr/starter-angularjs",
"version": "1.0.1",
"description": "AngularJS starter template",
"dependencies": {
"angular": "^1.6.9"
},
"plnkr": {
"runtime": "system",
"useHotReload": false
}
}
Alert message is not displayed.
By #Nick Parsons comment:
<script type="module" src="lib/script.js"></script>
This works.

javascript program not appending markup correctly

I am following a tutorial in Jasmine and serverless. The problem I am facing is around Javascript.
There is a public directory which has an index.html
Jasmine tests are in public/tests directory. It also has an index.html.
Following JS code (SpecHelper.js below) is suppose to find markups with class markup in public/index.html and copy that code in <body> of public/tests/index.html but it isn't doing so. I am unable to find the issue.
public/index.html
<body>
<div class='markup'>
<div class='view-container container'>
<div class='one-half column'>
<h3>Learn JS, one puzzle at a time</h3>
<a href='' class='button button-primary'>Start now!</a>
</div>
<div class='one-half column'>
<img src='/images/HeroImage.jpg'/>
</div>
</div>
</div>
</body>
SpecHelper.js
var fixture;
function loadFixture(path) {
var html;
jQuery.ajax({
url: '/index.html',
success: function(result) {
html = result;
},
async: false
});
return $.parseHTML(html);
}
function resetFixture() {
if (!fixture) {
var index = $('<div>').append(loadFixture('/index.html'));
var markup = index.find('div.markup');
fixture = $('<div class="fixture" style="display: none">').append(markup);
$('body').append(fixture.clone());
} else {
$('.fixture').replaceWith(fixture.clone());
}
}
beforeEach(function () {
resetFixture();
});
public/tests/index.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jasmine Spec Runner v2.3.4</title>
<link rel="shortcut icon" type="image/png" href="lib/jasmine-2.3.4/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine-2.3.4/jasmine.css">
<!-- App Dependencies -->
<script src="lib/jquery-2.1.4.js"></script>
<script src="/vendor.js"></script>
<!-- Test libraries -->
<script type="text/javascript" src="lib/jasmine-2.3.4/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-2.3.4/jasmine-html.js"></script>
<script type="text/javascript" src="lib/jasmine-2.3.4/boot.js"></script>
<!-- include source files here... -->
<script type="text/javascript" src="/app.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="SpecHelper.js"></script>
<script type="text/javascript" src="app_spec.js"></script>
</head>
<body>
<!--This is always empty! -->
</body>
</html>
The test in Jasmine fails with following error
1 spec, 1 failure
Spec List | Failures
learnJS can show problem view
Expected 0 to equal 1.
Try this
fixture = $('<div class="fixture" style="display: none">').append(markup.html());
Then maybe
$('body').append(fixture.clone().html());
got it working..phew ... had to clear browser cache by going to developer tools, networks. I noticed that most files were being picked from memory. Right clicked and cleared cache and cookies.

Provide factory of one module to another module

I have just started working on Angularjs and facing a problem in calling the factory defined in other module. Actually I need to clean the code so I have to build all the functionalities in one js file and I have to use them in my main js file where I have defined my controllers. I have made a simple code to understand the problem. Thanks in advance
Following is the Html file:-
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js#1.1.5" data-semver="1.1.5" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="function.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="MyServiceModuleOne">
<div ng-controller="TestController">
<button type="button" ng-click="getFn()"> Test </button>
</div>
</body>
</html>
Following is script.js file
var myModule= angular.module('MyServiceModuleTwo',['MyServiceModuleOne']);
myModule.controller('TestController', ['$scope', 'notify', function($scope, notify){
$scope.getFn = function() {
notify.sampleFun();
}
}]);
Following is function.js file:-
var myModule = angular.module('MyServiceModuleOne', []);
myModule.factory('notify', function() {
return {
sampleFun: function() {
alert('hi');
}
};
});
There are couple of changes needed.
You are referencing MyServiceModuleOne as root module, but the controller you are referencing is in MyServiceModuleTwo.
So change your ng-app from MyServiceModuleOne to MyServiceModuleTwo.
I've updated the plnkr

Simple ng-click not working in typescript

I'm trying to call a function in click function from my html page ,
added all typescript definition files from nuget but something is going wrong
My Click Function is not Working .... No error in console even
Here is my Hrml and Controller Code
Html Page
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Scripts/angular.js"></script>
<script src="test.js"></script>
<title></title>
</head>
<body ng-app="testModule">
<div ng-controller="test">
<input type="button" ng-click="click()" value="test" />
</div>
</body>
</html>
Controller
angular.module("testModule", []);
class test {
constructor() { }
click() {
alert();
}
}
angular.module("testModule").controller("test", test );
This does not work because ng-click="click()" tries to call $scope.click() which is not defined.
I would highly advise you to use the controller as-Syntax when working with AngularJS and Typescript
Demo
Here is the corrected code. Don't mark this as the answer, #Aides got here before me.
Html Page
<!DOCTYPE html>
<html> <!-- its 2016 -->
<head>
<script src="Scripts/angular.js"></script>
<script src="test.js"></script>
<title></title>
</head>
<body ng-app="testModule">
<div ng-controller="Test as test">
<input type="button" ng-click="test.click()" value="test" />
</div>
</body>
</html>
Controller
angular.module("testModule", []);
class Test {
click() {
alert();
}
}
angular.module("testModule").controller("Test", Test );

Angular2 doesn't work

I am new in Angular, so I choose Angular2.
I was following official guide here
Here is code
index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/2.0.0-alpha.34/angular2.sfx.dev.js"></script>
<script src="app.js"></script>
</head>
<body>
<my-app></my-app>
</body>
</html>
app.js
var AppComponent = ng
.Component({
selector: 'my-app'
})
.View({
template: '<h1 id="output">My First Angular 2 App</h1>'
})
.Class({
constructor: function () { }
});
Then I type live-server --port=8000, but it display nothing.
You need to check if DOM is ready with DOMContentLoaded first and then Bootstrap the app.
Official Angular 2.0 has mentioned to bootstrap.
Also as Jorg said:
"Angular 2 is currently in Developer Preview. We recommend using Angular 1.X for production applications"
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script src="https://code.angularjs.org/2.0.0-alpha.37/angular2.sfx.dev.js"></script>
<script src="main.js"></script>
</head>
<body>
<my-app></my-app>
</body>
</html>
Javascript (ES5):
var AppComponent = ng
.Component({
selector: 'my-app'
})
.View({
template: '<h1 id="output">My First Angular 2 App</h1>'
})
.Class({
constructor: function () { }
});
document.addEventListener('DOMContentLoaded', function() {
ng.bootstrap(AppComponent);
});
Plunker Here
can you add ng.bootstrap(AppComponent, []); to the bottom on app.js and update your angular version to
<script src="https://code.angularjs.org/2.0.0-alpha.37/angular2.sfx.dev.js"></script>

Categories