


AngularJS’s custom instructions are your own instructions, plus the native core functions that the compiler runs when compiling the DOM. This can be difficult to understand. Now, let's say we want to reuse some specific code across different pages in our application without duplicating the code. Then, we can simply put this code into a separate file and call the code using the custom directive instead of typing it out over and over again. Such code is easier to understand. There are four types of custom directives in AngularJS:
- Element command
- Attribute command
- CSS class directive
- Comment command
Before implementing them in our existing app, let’s take a look at what custom directives look like:
Element Command
Write the following tag in html, which is used to place code snippets. When we want to use a specific code, we include the code using the above tag.
<guitar-reviews> ... </guitar-reviews>
In the JS file, use the following lines of code to make the above angularJS custom directive effective.
app.directive('guitarReviews', function() { return { restrict : 'E', // used E because of element templateUrl : 'custom-directives/reviews.html' }; });
Code explanation:
Like app.controller, we first define app.directive, and then define guitarReview, which is the element tag name used in html. But have you noticed that guitar-review and guitarReviews are different? This is because the hyphen in guitar-reviews is converted to camel case, so it becomes guitarReviews in the JS file. The next step is the anonymous function that is returning parameters. restrict: 'E' means that we are defining a custom element directive, and templateUrl points to the code snippet file to be included.
Attribute directive
Type the following attributes in the html tag of the html file. This tag is used to hold code snippets. When we want to use a specific piece of code, we just type a tag like this to include the code.
<div guitar-reviews> ... </div>
In the JS file, use the following code to make the above angularJS custom directive effective.
app.directive('guitarReviews', function() { return { restrict : 'A', // used A because of attribute templateUrl : 'custom-directives/reviews.html' }; });
Note: AngularJS recommends that you use simple css and ordinary comments instead of CSS and comments in custom directives.
Now let’s implement custom commands in the app. You can download the project files here. I put the code for the reviews part into a separate file, then assigned the code snippet to an element, and finally used it in the details.html page.
First step
Create a new folder named cDirectives under the specified folder to store customized instructions. Then, create a reviews.html file in this folder to hold the user's reviews. At this point, your folder hierarchy looks like this:
Step 2
Cut the review section in details.html and add the
Step 3
Copy the code you cut in the details.html page to reviews.html as shown below:
<!-- Review Tab START, it has a new controller --> <div ng-show="panel.isSelected(3)" class="reviewContainer" ng-controller="ReviewController as reviewCtrl" > <!-- User Reviews on each guitar from data.json - simple iterating through guitars list --> <div class="longDescription uReview" ng-repeat="review in guitarVariable[whichGuitar].reviews"> <h3 id="Review-Points-review-star">Review Points: {{review.star}} </h3> <p> {{review.body}} ~{{review.name}} on <date>{{review.createdOn | date:'MM/yy'}} </p> </div><!-- End User Reviews --> <!-- This is showing new review preview--> <div ng-show="add === 1" class="longDescription uReview" > <h3 id="Review-Points-reviewCtrl-review-star-span-ng-click-add-X-span">Review Points: {{reviewCtrl.review.star}} <span ng-click="add=0">X</span></h3> <p> {{reviewCtrl.review.body}} ~ {{reviewCtrl.review.name}} </p> </div> <!-- Add new Review to specific guitar - click this link to show review adding pannel --> <a href ng-click="add=1" class="addReviewLink">Add review</a> <!-- form validates here using form name and .$valid and on submission we are going to addReview function with guitarID --> <form class="reviewForm" name="reviewForm" ng-submit="reviewForm.$valid && reviewCtrl.addReview(guitarVariable.indexOf(guitarVariable[whichGuitar]))" novalidate ng-show="add===1" > <div> Review Points: <!-- ng-option here is setting options, cool? --> <select ng-model="reviewCtrl.review.star" ng-options="point for point in [5,4,3,2,1]" required > </select> Email: <input type="email" ng-model="reviewCtrl.review.name" required> <button type="submit">Submit</button> </div> <textarea placeholder="Enter your experience with this guitar..." ng-model="reviewCtrl.review.body"></textarea> </form><!-- END add new review --> </div><br /><!-- END Review Tab -->
Step 4
It is now possible to add actions in the user-reviews tag. Let’s open controller.js and add the following code:
GuitarControllers.directive('userReviews', function() { return { restrict : 'E', // used E because of element templateUrl : 'partials/cDirectives/reviews.html' }; });
Code explanation:
Our
We just customized a directive. Even though it looks like nothing has changed in our application, our code is now better planned than before. Can you customize directives for descriptions and specifications? Try it yourself.

Javascript 是一个非常有个性的语言. 无论是从代码的组织, 还是代码的编程范式, 还是面向对象理论都独具一格. 而很早就在争论的Javascript 是不是面向对象语言这个问题, 显然已有答案. 但是, 即使 Javascript 叱咤风云二十年, 如果想要看懂 jQuery, Angularjs, 甚至是 React 等流行框架, 观看《黑马云课堂JavaScript 高级框架设计视频教程》就对了。

在如今信息时代,网站已经成为人们获取信息和交流的重要工具。一个响应式的网站能够适应各种设备,为用户提供优质的体验,成为了现代网站开发的热点。本篇文章将介绍如何使用PHP和AngularJS搭建一个响应式网站,从而提供优质的用户体验。PHP介绍PHP是一种开源的服务器端编程语言,非常适用于Web开发。PHP具有很多优点,如易于学习、跨平台、丰富的工具库、开发效

随着互联网的不断发展,Web应用已成为企业信息化建设的重要组成部分,也是现代化工作的必要手段。为了使Web应用能够便于开发、维护和扩展,开发人员需要选择适合自己开发需求的技术框架和编程语言。PHP和AngularJS是两种非常流行的Web开发技术,它们分别是服务器端和客户端的解决方案,通过结合使用可以大大提高Web应用的开发效率和使用体验。PHP的优势PHP

随着互联网的普及和发展,前端开发已变得越来越重要。作为前端开发人员,我们需要了解并掌握各种开发工具和技术。其中,PHP和AngularJS是两种非常有用和流行的工具。在本文中,我们将介绍如何使用这两种工具进行前端开发。一、PHP介绍PHP是一种流行的开源服务器端脚本语言,它适用于Web开发,可以在Web服务器和各种操作系统上运行。PHP的优点是简单、快速、便

随着互联网的普及,越来越多的人在使用网络进行文件传输和共享。然而,由于各种原因,使用传统的FTP等方式进行文件管理无法满足现代用户的需求。因此,建立一个易用、高效、安全的在线文件管理平台已成为了一种趋势。本文介绍的在线文件管理平台,基于PHP和AngularJS,能够方便地进行文件上传、下载、编辑、删除等操作,并且提供了一系列强大的功能,例如文件共享、搜索、

随着Web应用程序的普及,前端框架AngularJS变得越来越受欢迎。AngularJS是一个由Google开发的JavaScript框架,它可以帮助你构建具有动态Web应用程序功能的Web应用程序。另一方面,对于后端编程,PHP是非常受欢迎的编程语言。如果您正在使用PHP进行服务器端编程,那么结合AngularJS使用PHP将可以为您的网站带来更多的动态效

随着Web技术的飞速发展,单页Web应用程序(SinglePageApplication,SPA)已经成为一种越来越流行的Web应用程序模型。相比于传统的多页Web应用程序,SPA的最大优势在于用户感受更加流畅,同时服务器端的计算压力也大幅减少。在本文中,我们将介绍如何使用Flask和AngularJS构建一个简单的SPA。Flask是一款轻量级的Py


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
