In the past few months, I have extensively written about two related but seemingly contradictory points. The first thing is that learning JavaScript MVC framework does not mean learning JavaScript in depth. The second is angularjs, which is amazing. I like it and everyone should do the same. If you want to learn more about JavaScript, please listen to classes, read books and learn the language. If you want to make something handsome, please learn angular as soon as possible. However, don’t skip the basics of JavaScript. I think it’s quite easy to learn angular, but that’s because I understand the basics of JavaScript. If this is the first JavaScript article, this article does not apply. For everyone else, here is a quick guide to launching the angularjs and WordPress rest APIs. The
Angularjs uses restful APIs, such as the WordPress rest API. To learn angular, whether word press or other APIs, you must understand the API. I actually started angular using the firebase API and the angular client. Firebase is a great service. After understanding the basic knowledge of angular, you must read Roy Sivan’s torque article on using firebase in WordPress. Angular is more than just an API. To get started, you must start with data operations on objects that forget the API and hard code into a file. After knowing the templates, controllers, and instructions, you can work in the HTTP interface of angular and use it to connect to the remote API. The
Angular uses the model view controller mode, so it is difficult to choose a location to start describing how angular works. If you are not familiar with the MVC pattern, consider the following: A view or template defines a visual representation of the data. The controller is an intermediary that uses a remote API to keep the model up to date. The model is also updated based on interaction with the view. A model is the current data set defined by the controller and displayed by the view. To start an angular application, you need at least a controller and a template. First the template, then the controller. If this is the first time, you may need to read the part of the template so that it is not understood, read the part of the controller, and then read the part of the template again. Remember that this content is not very linear. And we’re not close to Ajax yet. The
This article will provide a simple and basic introduction to angularjs. Prepare to use angular with WordPress rest API or restful API to start development. In the startup template and binding angular, I like that the template is simple HTML, which is bound with HTML5. Some frameworks provide a completely new way to write HTML, but do not provide angular. For example, if you want the view to contain HTML elements that contain the \
Male or female angular keeps the view (V of MVC) as HTML. Similarly, PHP is a tool for generating HTML, so you can create template files in PHP as usual. This is not always the best approach, but it is sometimes necessary. Binding connects the view to the controller. In the view, bindings are generated by HTML 5 data attributes or template tags enclosed in double brackets. Here are two simple examples to illustrate binding: The first is to use the angle indicator as the HTML5 data attribute to bind the fields in the model to the form input. The
Male breeding
Although the input looks like other form input, it is a post of the current model. The title contains the other data attribute \
The two can be combined as follows: Male
Male
Male
Male
Male {{post.title} male
If the controller is set, you can enter the value in the input and see the magic display in the following container. The
In the controller MVC mode, the controller is used to define the model and provide the logic or other functions required within the view. Now only the post title is processed. We will use a hard coded model, but as you can imagine, we are slowly importing the word press rest API. In the previous section, we created a very simple view without controllers. Now let’s set up a basic angular application that contains controllers that can insert models into this view to view and edit \
(function (angle){
\
Angular Module (\
}) (window. Angle); The third line of the code assumes that the page contains angular. Create an application named \
There are applications now, but you need to add controllers. There are several ways to handle the syntax for adding controllers and other components to an application. In this report, \
Controller is empty: (function (angle){
\
Angular Module (\
Controller (‘postexample’, [‘$scope’, function ($scope){
}]);
}) (window. Angle); It is important to understand that we injected $scope into this controller called \
When talking about views, {{post.title} is used, which assumes that $scope has a post named title. Let’s add the following: (function (angle){
\
Angular Module (\
Controller (‘postexample’, [‘$scope’, function ($scope){
$scope Post ={
Title: enter title
};
}]);
}) (window. Angle); Now you can combine it with the view in the last section and magically update the input different elements. If you want to see the sample merged into an HTML file and work, you can do so in the plugin. In fact, I strongly recommend that you open it and start the experiment yourself. Furthermore, more bound angular have many basic controllers. Angular has excellent documents, so it is listed.
I won’t. I would like to introduce the ng hide instruction. Let’s add a submit button to the form. It can be simple: But someone posted. Hide the value of title before changing it from the default value \
Male
Male
Male
Male
Male {{post.title} male
In the male controller, add the submit function to $scope:$ Scope Submit= function (){
Warning (\
}The
This will warn you when the form is submitted. It is sufficient to understand the binding between the controller and the view. It also provides the basics needed to create a model based on content imported from the rest API. To do this, you must use the $http service of angular, which will be introduced in the torque article below. In addition, before starting the service, you need to learn a very important instruction called ng repeater, which is used to cycle between the array and the object. We hope you are modifying the previously shown plunker to repeat these changes at an angle. Now you should see: There is only one post in the current view. Of course, we want to list many posts and show them separately in the same way. We always do this in traditional word press themes. Use a loop to repeat all posts of the current wp\u query object. Angular provides an instruction called ng rear. This allows you to duplicate the set of items in scope. We will add new views and controllers to illustrate this. To get started, we will add a new controller named postsexample, which has multiple posts in $scope. The new controller includes: Controller (‘postexample’, [‘$scope’, function ($scope){
$scope Post ={
Title: enter title
};
}]) Controller (‘postsexample’, [‘$scope’, function ($scope){
$scope Posts =[
{Title: \
{Title: \