<span id="Label3"></p><p><p><span style="color: #ff0000; font-size: 14pt;"><strong>first, the beginning</strong></span></p></p><p><p>Really is busy after this a while can be busy for a while Ah ...</p></p><p><p>Recently doing a angularjs+ionic mobile project ... Record some skills, convenient for you to check later, but also convenient for people to take a look at ... ^_^</p></p><p><p><span style="font-size: 14pt;"><strong><span style="color: #ff0000;">Ii. understanding of Basic principles</span></strong></span></p></p><p><p><span style="font-size: 16px; color: #0000ff;"><strong>①angular Some of the Getting started understanding</strong></span></p></p><span style="font-size: 16px;"><span style="font-size: 16px;"><strong>first, the basic knowledge</strong></span></span>1.angular abandoned IE82. The four core are mvc, modularization, instruction system, two-way data binding, respectively.<span style="font-size: 18px;"><span style="font-size: 18px;"><strong>Ii. Some of the principles</strong></span></span>1. Do not reuse controllers, a controller is generally responsible for only a small block of views. 2. Do not manipulate the DOM inside the Controller. 3. Do not format data in contorller, ng has a good form Control. 4. Do not do data filtering operation in controller, have $filter service. 5. In general, controllers do not call each other, and the interaction between them occurs through Events. 6.angular use command to reuse VIEW. 7. $scope is a tree structure that is parallel to the DOM Tag. 8. The child $scope object inherits the properties and methods on the parent $scope. 9. Each angular application has only one $rootscope Object. (usually located on ng-app). 10. You can debug with angular.element ($) scope (). 11. Use Ngroute to route between Views.<p><p><span style="font-size: 16px; color: #0000ff;"><strong></strong></span></p></p><p><p></p></p><p><p><span style="font-size: 14pt; color: #ff0000;"> <strong>third, HTML page <strong>most commonly used and practical angular built-in instructions</strong> </strong></span></p></p><p><p><span style="color: #ff00ff; font-size: 15px;"><strong>①.ng-class (for a style similar to likes, concerns, etc. that may change because of an action)</strong></span></p></p><p><p>the <strong>ng-class</strong> directive is used to dynamically bind one or more CSS classes to an HTML Element. <code>ng-class</code>the value of the directive can be a string, an object, or an array.</p></p><p><p>If it is a string, multiple class names are separated by Spaces.</p></p><p><p>If it is an object, you need to use the Key-value pair, and key is a Boolean value, which is the class name you want to add. The class is added only if key is True.</p></p><p><p>If it is an array, it can consist of a string or an object combination, and the elements of the array can be strings or objects.</p></p><p><p>Recommended two ways to Use:</p></p><p><p>first, the string form, the code is as Follows:</p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"><i class= "icon" ng-class= "{true: ' ion-ios-heart ', false: ' ion-ios-heart-outline '}[accountinfo.isfocus]" ng-click= ' Wetherfocus () ' ></i></pre></pre><p><p>This means that the I tag has a base class of icon,ng-class that binds a dynamic class, The value of this class is determined by whether the value of Accountinfo.isfocus is true or false, and if its value is true the I tag will add the class ion-ios-heart, and if its value is false the I label will be added Ion-ios-heart-outline</p></p><p><p>This class. The I tag also binds a ng-click event, which, in addition to handling the corresponding logic, determines the value of the Accountinfo.isfocus. In this case, when the click action occurs, it is natural to change the I tag corresponding class, and then show a different style.</p></p><p><p>second, the Key-value style, the code is as Follows:</p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"><i class= "icon" ng-class= "{' ion-ios-heart ': isios, ' Ion-android-heart ': isandroid}" > </i></pre></pre><p><p>obviously, it can be seen from the code that the implication is that when Isios is true, the Ion-ios-heart class is taken, and when the value of Isandroid is true, Ion-android-heart is Taken.</p></p><p><p><span style="color: #ff00ff; font-size: 15px;"><strong>②.ng-show and Ng-hide (for different scenarios when displaying two different content)</strong></span></p></p><p><p>the <strong>ng-show</strong> instruction displays the specified HTML element when the expression is true, otherwise hides the specified HTML Element.</p></p><p><p>the <strong>ng-hide</strong> directive hides the specified HTML element when the expression is true, otherwise the specified HTML element is Displayed.</p></p><p><p>haha, looking at is the Fire-and-fire brothers ....</p></p><p><p>Examples are as Follows:</p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"><div class= "keyboard" > <span class= "keyboardicon" ng-click= "togglemenu ()" ></span></div ><div class= "footer-menu" > <ul class= "menu-list" ng-show= "menustate" > ... </ul><div class= "footer-send" ng-hide= "menustate" > ...</div></pre></pre><p><p>Set a Boolean variable menustate (you can use expressions, trinocular, and so on in Real-world development), and when it is true, the contents of Ng-show will be displayed and the contents of Ng-hide will be Hidden. conversely, the converse ...</p></p><p><p><span style="font-size: 15px;"><strong><span style="color: #ff00ff;">③.ng-switch (for displaying different content in a variety of SITUATIONS)</span></strong></span></p></p><p><p>the <strong>ng-switch</strong> Instruction shows or hides the corresponding part based on an Expression.</p></p><p><p>The corresponding child elements use the <strong>ng-switch-when</strong> directive, and if the match is selected, the selection is displayed and the others are Removed.</p></p><p><p>By setting the default options by using the <strong>ng-switch-default</strong> directive, The default option is displayed if there are no matching cases.</p></p><p><p>Example:</p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"> <div ng-switch= "essaytype" > <div class= "list-cart" ng-switch-when= "4" > .... </div> <div class= "list-cart left-pic" ng-switch-when= "3" > .... </div> <div class= "single-pic" ng-switch-when= "1" > ... </div> <div class= "single-pic" ng-switch-when= "2" > ... </div> <div class= "single-pic" ng-switch-default> ... </div></div></pre></pre><p><p><span style="font-size: 15px;"><strong><span style="color: #ff00ff;">④.ng-model (here's a little bit about Ng-model's magical Pits)</span></strong></span></p></p><p><p>the <strong>ng-model</strong> directive binds an HTML form element into the scope Variable.</p></p><p><p>If a variable does not exist in scope, it will be created. Ng-model is commonly used in <input>, <select>, <textarea> and other Elements.</p></p><p><p>The following code:</p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"><div class= "whatisay" > <textarea name= "my-massage-detail" ng-model= "content" class= "my-massage-detail "placeholder=" Please enter message "> </textarea> <a class=" button btn "ng-click=" submitmes () "> Submit </a ><br></div></pre></pre><p><p>By definition, in theory, when we commit, it is possible to get the value of the Ng-model defined in the page directly in the Controller. But in practice this is not feasible. the <span style="color: #ff0000;">pro-test Discovery outputs a undefined, and if the initial value of Ng-model is defined in the controller, the initial value is obtained instead of the updated value after the Change. </span></p></p><p><p><strong><span style="color: #ff0000;">Looked up some information, probably meaning. Angular limits some of our definitions. We can only use a Non-primitive object to pass Parameters.</span></strong></p></p><p><p><span style="color: #000000;"><strong>What do you mean? Change the above example slightly, as Follows:</strong></span></p></p><p><p><span style="color: #000000;"><strong>HTML code:</strong></span></p></p><pre class="brush:html;gutter:true;"><pre class="brush:html;gutter:true;"><div class= "whatisay" > <textarea name= "my-massage-detail" ng-model= "model.content" class= " My-massage-detail "placeholder=" Please enter message "> </textarea> <a class=" button btn "ng-click=" submitmes ( ) "> Submit </a></div></pre></pre><p><p>Controller code:</p></p><pre class="brush:javascript;gutter:true;"><pre class="brush:javascript;gutter:true;"> $scope. model = {}; $scope. model.content = "; $scope. submitmes=function () { console.log ($scope. model.content); }</pre></pre><p><p>Is that we define an object, and then define the Ng-model as a property within the object to Handle. This way, we get the newest value of Ng-model.</p></p><p><p><strong>There is also a relatively simple way is to direct the Ng-model as a parameter in the Good.</strong></p></p><p><p>Examples are as Follows:</p></p><pre class="brush:javascript;gutter:true;"><pre class="brush:javascript;gutter:true;">HTML code <input type= "text" ng-model= "code" ><button ng-click= "setcode (code)" >Login</button><br>Controller code $scope.setcode = function (code) { alert (code); }</pre></pre><p><p><span style="color: #ff0000; font-size: 14pt;"><strong>Iv. practical techniques for data interaction</strong></span></p></p><p><p><span style="color: #ff00ff;"><strong>The use of ①promise</strong></span></p></p><p><p><strong>ES6 defines the Promise Object. This object is very useful, especially when interacting with the Background. It prevents the callback from being too deep, and it can be handled uniformly for some situations, and also improves the readability of the Code. In the ANGULARJS also encapsulated such a service, is $Q.</strong></p></p><p align="left"><p align="left"><strong>$Q exists as a service for angularjs, just a simplified implementation version of the Promise asynchronous programming Pattern. The defer object (lingering Object) can be obtained through $q.defer (), which has three methods:</strong></p></p><p align="left"><p align="left"><strong>Resolve (value):</strong> sends a message to the Promise object asynchronous execution body tell him I have successfully completed the task, value is the message Sent.</p></p><p align="left"><p align="left"><strong>reject (value):</strong> sends a message to the Promise object asynchronous execution body tell him I can't finish this task, value is the message Sent.</p></p><p align="left"><p align="left"><strong>Notify (value):</strong> sends a message to the Promise object asynchronous execution body tell him what I'm doing now, value is the message Sent.</p></p><p align="left"><p align="left">After these messages are sent, promise calls the existing callback Function.</p></p><p align="left"><p align="left"><strong>Promise is the committed object with this defer object. Promise objects can be obtained through defer.promise, promise some methods of the Object:</strong></p></p><p><p>then <strong>(successcallback,errorcallback,notifycallback):</strong> parameters are different callback functions under different messages, defer send different messages to execute different callback functions, Messages are passed as arguments to these callback Functions. The return value is back to a promise object that exists to support chained Calls. When the first defer object sends a message, the subsequent promise corresponding defer object also sends the message, but the message is sent differently, regardless of whether the first defer object is sending reject or resolve, and the second and later are sent resolve, The message is Transitive.</p></p><p><p><strong>catch (errorcallback):</strong>then (null,errorcallback) abbreviation.</p></p><p><p><strong>finally (callback):</strong> equivalent to then (callback,callback) abbreviation, the method in this finally does not accept parameters, but can defer sent message and message type successfully passed to the next Then.</p></p><p align="left"><p align="left"><strong>defer ():</strong> used to generate a deferred object var defer = $q. defer ();</p></p><p align="left"><p align="left"><strong>reject ():</strong> The parameter receives an error message, which is equivalent to throwing an exception in the callback function and then invoking the wrong callback function in the next Then.</p></p><p align="left"><p align="left">all <strong>()</strong> <strong>:</strong> The parameter is received as a promise array, and a new single Promise object is returned, and when these promise objects correspond to the defer object, the single Promise object is resolved, When one of these promise objects is reject, The single promise is also Reject.</p></p><p align="left"><p align="left">when <strong>():</strong> receives the first parameter as an arbitrary value or a promise object, and the other 3 promise then methods, the return value is a promise Object. If the first parameter is not a promise object, the success callback is run directly and the message is the object, and if promise then the promise returned is a wrapper over the parameter of the Promise Type. The message sent by the promise corresponding to the defer will be received by the Promise object returned by our when Function.</p></p><p align="left"><p align="left">The specific usage is as Follows:</p></p><p align="left"><p align="left">In angular, define a service that is designed to Interact.</p></p><pre class="brush:javascript;gutter:true;"><pre class="brush:javascript;gutter:true;"> <br> var deferred = $q. defer (); <br> Showtip (); $HTTP. get (url, options). success (function (data) { hidetip (); If (data. Success) { deferred.resolve (data); } else { deferred.reject (data). Message); } }). Error (function (data) { hidetip (); Deferred.reject (data); }); Return deferred.promise; }//controller inside the call get (' URL ', params). then (data) { //here is Successcallback },function (data) { //this is Errorcallback });</pre></pre><p><p>In this way, we can define some hints uniformly when each request is issued, and then hide the hints after the request is Finished. In this code, the general meaning is that when the request succeeds, it calls Deferred.resolve (data), sets the state to success, and then automatically executes the first function in then, the successcallback, and pass the requested data in. When the request fails, a second function is called, errorcallback.</p></p><p><p> </p></p><p><p>Let's summarize here for the time being ... 6_6</p></p><p><p>If you think my summary to help you, please scan the following QR code, give me a little encouragement 9_9</p></p><p><p></p></p><p><p></p></p><p><p>Angularjs Practical Development Skills</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service