• No se han encontrado resultados

LOS JÓVENES VENEZOLANOS ANTE LA SOCIEDAD DE HOY

Rosa Aparicio Gómez*

LOS JÓVENES VENEZOLANOS ANTE LA SOCIEDAD DE HOY

In this section we‘ll discuss an app we‘ll call myApp. You can follow along with our series by git cloning the repository (instructions below) or by following along with our instructions.

Create an index.html file with the following content:

<!doctype html>

<html ng-app="myApp">

<head>

<script

src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.

js"></script>

<script src="js/main.js"></script>

</head>

<body>

</body>

</html>

Then, make a directory called js and make a file in that directory called main.js, like so:

mkdir js

touch js/main.js

This HTML page will load both AngularJS and our app that we‘ll write in main.js. Almost all of the work that we will do in this section will be in the main.js file.

In order to extend and customize our app, we‘ll need to write some JavaScript. All the JavaScript that we will write will go into our main.js file.

angular.module

To define an AngularJS app, we first need to define an angular.module. An Angular module is simply a collection of functions that are run when the application is ―booted.‖

(We‘re not going to discuss configuration and run blocks in this series, but we‘ll address them in future sections.)

Next, we need to define the module in our main.js file:

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

This line creates the Angular module named myApp. (Don‘t worry about the second argument – the empty array, [] – for now. We‘ll get to it.)

Now, we want to instantiate our myApp module on our page and tell Angular where in the DOM tree our app lives. To instantiate the module in a page, we‘ll be using the

ng-app directive, which tells Angular that we want our module to own that part of the DOM tree.

We can instantiate our own app (module) by simply passing the name of our app as the value in our index.html file:

<html ng-app="myApp">

When we refresh the page, Angular will bootstrap myApp.

We can set ng-app on any element in the DOM, and that‘s where Angular will launch on the page. This step is how we can write an Angular app inside of any web page, even if the rest of the application isn‘t written in Angular.

<h2>I am not inside an AngularJS app</h2>

<div ng-app="embeddedApp">

<h3>Inside an AngularJS app</h3>

</div>

For an app that will take over the entire page, you can place the ng-app directive on the html element.

Once we‘ve defined our app, we can start building out the rest of the application. We‘ll build using $scope, which is one of the most important Angular concepts. We will cover

the $scope service in depth in Part 2 of our 7-part series.

So there we have it - the basic structure for an Angular app. We‘ll use this as a starting point to build our NPR player.

Update: Read the next section of the series here.

The official repository for the beginner series is available as a git repo here: https://github.com/auser/ng-newsletter-beginner-series.

To get this repo locally, ensure you have git installed, clone the repo, and check out the part1 branch:

git clone https://github.com/auser/ng-newsletter-beginner-series.git

git checkout -b part1

Want to know when Part 2 and every article in this series is released? Sign up for ng-newsletter below.

This is the second post of AngularJS - from beginner to expert in 7 steps.

We started our first post by showing you how to start out building an AngularJS app. In this post, we‘ll discuss a fundamental concept in understanding how AngularJS works and how you can use it to your advantage.

Throughout this tutorial series, we are going to be building an NPR audio player that will show us the current stories on the show Morning Edition and play them in our browser. To see the fully finished demo, head over here.

Part 2. Scopes

A $scope is an object that ties a view (a DOM element) to the controller. In the Model-View-Controller structure, this $scopeobject becomes the model. It provides an execution

context that is bound to the DOM element (and its children).

Although it sounds complex, the $scope is just a JavaScript object. Both the controller and the view have access to the $scopeso it can be used for communication between the two.

This $scope object will house both the data and the functions that we‘ll want to run in the view, as we‘ll see.

All AngularJS apps have a $rootScope. The $rootScope is the top-most scope that is created on the DOM element that contains the ng-app directive.

This is where AngularJS binds data and functions when explicit $scopes are not set in the page. This is why the example inpart 1 works.

In this example, we‘re working with the $rootScope. We add an attribute on the scope of name in our main.js file. By putting this function in the app.run function, we‘ll

guarantee that it will run prior to the rest of the app. You can think of the runfunction being the main method of the angular app.

app.run(function($rootScope) { $rootScope.name = "Ari Lerner";

});

Now, anywhere in our view, we can reference this attribute using the expression/template: {{

}}, like so:

{{ name }}

We‘ll go more in-depth into how the expression template syntax works later in this series.

See it:

Ari Lerner In summary:

Anything that we attach to this $scope object will become available to the view.

Likewise, any changes that the controller makes to the model will show up as changes in the view.

To really see the power of scopes, let‘s attach a controller to a DOM element, which will create a $scope for the element and allow us to interact with it.

ng-controller

To explicitly create a $scope object, we‘ll attach a controller object to a DOM element using the ng-controller directive on an object, like so:

<div ng-controller="MyController">

{{ person.name }}

</div>

The ng-controller directive creates a new $scope object for the DOM element and nests it in the containing $scope. In this example, the parent $scope of the div with

ng-controller is the $rootScope object. The scope chain looks like this:

Now, MyController sets up a $scope that we can access from inside the DOM element. In this case, we‘re going to create a person object on the $scope of MyController, in main.js:

app.controller('MyController', function($scope) { $scope.person = {

name: "Ari Lerner"

};

});

Now we can access this person object in any child element of the div where ng-controller='MyController' is written because it is on the $scope.

See it

Ari Lerner

With one exception, all scopes are created with prototypal inheritance, meaning that they have access to their parent scopes. By default, for any property that AngularJS cannot find on

a local scope, AngularJS will crawl up to the containing (parent) scope and look for the property or method there. If AngularJS can‘t find the property there, it will walk to that scope‘s parent and so on and so forth until it reaches the $rootScope.

The one exception: Some directives can optionally create an isolate scope and do not inherit from their parents.

For instance, say we have a ParentController that contains the user object and a ChildController that wants to reference that object:

app.controller('ParentController', function($scope) { $scope.person = {greeted: false};

});

app.controller('ChildController', function($scope) { $scope.sayHello = function() {

$scope.person.greeted = true;

} });

When we bind the ChildController under the ParentController in our view, we can reference the property on the parent scope just as if it were on the ChildController:

<div ng-controller="ParentController">

<div ng-controller="ChildController">

<input type="text" ng-model="person.name"

placeholder="Name"></input>

<a ng-click="sayHello()">Say hello</a>

</div>

{{ person }}

</div>

See it

Say hello Reset {"greeted":false}

Documento similar