[AngularJS] Reusable directive, require from parent controller
Glorious Directives for Our Navigation
NoteWrangler navigation has now been broken into two parts: the children — nw-nav-item
— and the parent — nw-nav
.
Help the children and parent communicate by using what we have learned about $scope
and link
. They'll need to function as a nav should when clicked.
Create a default activeNav
variable on nwNav
's $scope
and make it default to'Notes'
.
angular.module('NoteWrangler')
.directive('nwNav', function() {
return {
restrict: 'E',
controller: function($scope) {
$scope.activeNav = 'Notes'; }
};
});
Create a function called getActiveNav
in the controller of the nw-nav
directive that returns the value of $scope.activeNav
variable.
angular.module('NoteWrangler')
.directive('nwNav', function() {
return {
restrict: 'E',
controller: function($scope) {
$scope.activeNav = 'Notes';
this.getActiveNav= function(){
return $scope.activeNav;
};
}
};
});
Create a function called setActiveNav
on the controller of the nw-nav
directive that sets the value of $scope.activeNav
variable.
angular.module('NoteWrangler')
.directive('nwNav', function() {
return {
restrict: 'E',
controller: function($scope) {
$scope.activeNav = 'Notes';
this.getActiveNav= function(){
return $scope.activeNav;
};
this.setActiveNav = function(note){
$scope.activeNav = note;
};
}
};
});
return this to the controller:
angular.module('NoteWrangler')
.directive('nwNav', function() {
return {
restrict: 'E',
controller: function($scope) {
$scope.activeNav = 'Notes';
this.getActiveNav= function(){
return $scope.activeNav;
};
this.setActiveNav = function(note){
$scope.activeNav = note;
}; return this;
}
};
});
The Parent Is Required
The nwNavItem
directive needs to be able to communicate with the parentnwNav
directive in order to tell when a nav item should be active. Let's go ahead and set that up.
Within the nwNavItem
directive, use the require
option to gain access to the controller from the parent nwNav
directive.
angular.module('NoteWrangler')
.directive('nwNavItem', function() {
return {
restrict: 'E',
templateUrl: './templates/directives/nw-nav-item.html',
require: '^nwNav'
};
});
Give the nwNavItem
directive a link
function. Make sure to fill in all the arguments so that we have access to the controller required from the previous task.
angular.module('NoteWrangler')
.directive('nwNavItem', function() {
return {
restrict: 'E',
templateUrl: './templates/directives/nw-nav-item.html',
require: '^nwNav',
link: function(scope, element, attrs, nwNavCtrl){ }
};
});
Using Parent Functionality
We've created the isActive()
and activate()
functions on the scope of the nwNavItem
directive. Within these functions, we'll need to access the controller of the nwNav
directive to set and get which nav item is active.
First, we need a name for the nav item to work. Create a new isolate scope on thenwNavItem
directive and allow it to accept an attribute (@) value called name
.
angular.module('NoteWrangler')
.directive('nwNavItem', function() {
return {
restrict: 'E',
templateUrl: './templates/directives/nw-nav-item.html',
require: '^nwNav',
link: function(scope, element, attrs, nwNavCtrl) {
scope.isActive = function() { }; scope.activate = function() { };
},
scope: {
name: '@'
}
};
});
Within the isActive()
function, call the getActiveNav()
function from the required
controller to get the current active nav value. Compare the return value from the controller with the scope.name
value and return the result from the isActive
function.
angular.module('NoteWrangler')
.directive('nwNavItem', function() {
return {
restrict: 'E',
templateUrl: './templates/directives/nw-nav-item.html',
require: '^nwNav',
link: function(scope, element, attrs, nwNavCtrl) {
scope.isActive = function() {
return nwNavCtrl.getActiveNav()==scope.name;
}; scope.activate = function() { };
},
scope: {
name: '@'
}
};
});
We need a way to set the active nav value when a nav item is clicked. In ouractivate()
function, call the setActiveNav()
function on the require
'd controller and pass the scope.name
as an argument.
angular.module('NoteWrangler')
.directive('nwNavItem', function() {
return {
restrict: 'E',
templateUrl: './templates/directives/nw-nav-item.html',
require: '^nwNav',
link: function(scope, element, attrs, nwNavCtrl) {
scope.isActive = function() {
return nwNavCtrl.getActiveNav()==scope.name;
}; scope.activate = function() {
nwNavCtrl.setActiveNav(scope.name);
};
},
scope: {
name: '@'
}
};
});
The Magic Revealed
Now that we have our nav directives working, we need to hook up the templates.
We can see in the index.html
that we're already passing Notes
and Users
to thename
attribute of our nav item directive. Use data binding to display the value ofscope.name
as the content of our a
tag.
<a class="list-item" href="#/">{{name}}</a>
Give each nwNavItem
a class of active
if it isActive()
.
<a class="list-item" ng-class="{'active': isActive()}" href="#/">{{name}}</a>
On click, call the activate()
method.
<a class="list-item" ng-class="{'active': isActive()}" ng-click="activate()" href="#/">{{name}}</a>
Create an href
for each nav item with a dynamic path using the name
variable. The route should look like: #/value_of_scope_dot_name
and use data binding.
<a class="list-item" ng-class="{'active': isActive()}" ng-click="activate()" href="#/{{name}}">{{name}}</a>
Notice that routes are case sensitive. When we click on Users, it finds no route for/Users
, and therefore redirects to /notes
. Solve this issue by using a lowercase
filter on the name
binding within the route.
<a class="list-item" ng-class="{'active': isActive()}" ng-click="activate()" href="#/{{name | lowercase}}">{{name}}</a>
Since we've added an expression to the href
attribute of our a
tag, we need to change it to ng-href
attribute. Check out the docs to see why this is important.
<a class="list-item" ng-class="{'active': isActive()}" ng-click="activate()" ng-href="#/{{name | lowercase}}">{{name}}</a>
[AngularJS] Reusable directive, require from parent controller的更多相关文章
- AngularJS自定义Directive中link和controller的区别
在AngularJS中,自定义Directive过程中,有时用link和controller都能实现相同的功能.那么,两者有什么区别呢? 使用link函数的Directive 页面大致是: <b ...
- AngularJs 指令 directive中link,controller 的区别
其实严格来讲,link和controller是完全不同的概念,这里讲区别有点牵强. angular指令中,带有link和controller两个函数,很多人在写指令的时候不知道是写在link里 还是c ...
- AngularJs 指令directive之require
controller的用法分为两种情形,一种是require自定义的controller,由于自定义controller中的属性方法都由自己编 写,使用起来比较简单:另一种方法则是require An ...
- angularJS中directive与controller之间的通信
当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...
- AngularJS自定义Directive与controller的交互
有时候,自定义的Directive中需要调用controller中的方法,即Directive与controller有一定的耦合度. 比如有如下的一个controller: app.controlle ...
- AngularJS之directive
AngularJS之directive AngularJS是什么就不多舌了,这里简单介绍下directive.内容基本上是读书笔记,所以如果你看过<AngularJS up and runnin ...
- Angularjs之directive指令学习笔记(二)
1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...
- angularjs的directive详解
Directive(指令)笔者认为是AngularJ非常强大而有有用的功能之一.它就相当于为我们写了公共的自定义DOM元素或CLASS属性或ATTR属性,并且它不只是单单如此,你还可以在它的基础上来操 ...
- 【angularJS】Directive指令
AngularJS 通过被称为 指令 的新属性来扩展 HTML.指令是扩展的 HTML 属性,带有前缀 ng-. 内置指令 1.ng-app 指令初始化一个 AngularJS 应用程序. 定义了 A ...
随机推荐
- 【spring-boot】快速构建spring-boot微框架
spring-boot是一个快速构建环境的一套框架,其设计理念是尽可能的减少xml的配置,用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义 ...
- auto make System.map to C header file
#!/bin/bash # auto make System.map to C header file # 说明: # 该脚本主要是将Linux内核生成的System.map文件中的符号.地址存入结构 ...
- ↗☻【编写可维护的JavaScript #BOOK#】第8章 避免“空比较”
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- 【 D3.js 高级系列 — 3.0 】 堆栈图
堆栈图布局(Stack Layout)能够计算二维数组每一数据层的基线,以方便将各数据层叠加起来.本文讲解堆栈图的制作方法. 先说说什么是堆栈图. 例如,有如下情况: 某公司,销售三种产品:个人电脑. ...
- C++学习笔记:不用sizeof判断int类型占用几个字节
#include <stdio.h> #include <string.h> char *change(int val, int base, char *retbuf) { s ...
- Gradle使用手册(三):构建任务
原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1. ...
- 嵌入式Linux USB WIFI驱动的移植
硬件平台:飞思卡尔MX258开发板 操作系统:Linux2.6.31 WIFI: RT2860 USB WIFI模组 交叉编译环境:gcc version 4.1.2 调试步骤: 第一步:测试U ...
- HDU4865 Prince and Princess 强连通分量+二分图判定
这个题就是建图费点劲,别的和我上一篇博客一样 然后,参考思路请戳这里http://www.cnblogs.com/wally/archive/2013/09/12/3317883.html 补充:这个 ...
- SSH proxy
# for Linux ssh nobody@guoliangwu.com -P 22 -C -N -D 127.0.0.1:6500 # for windows(PuTTY) plink nobod ...
- ubuntu 11.10 (64bit) install opencv 2.4.8 and run in Qtcreator
install gtk2+ sudo apt-get install libgtk2.0*sudo apt-get install cmake-qt-gui tar xzvf opencv-2.4.8 ...