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 requiredcontroller 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 isActivefunction.

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 lowercasefilter 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的更多相关文章

  1. AngularJS自定义Directive中link和controller的区别

    在AngularJS中,自定义Directive过程中,有时用link和controller都能实现相同的功能.那么,两者有什么区别呢? 使用link函数的Directive 页面大致是: <b ...

  2. AngularJs 指令 directive中link,controller 的区别

    其实严格来讲,link和controller是完全不同的概念,这里讲区别有点牵强. angular指令中,带有link和controller两个函数,很多人在写指令的时候不知道是写在link里 还是c ...

  3. AngularJs 指令directive之require

    controller的用法分为两种情形,一种是require自定义的controller,由于自定义controller中的属性方法都由自己编 写,使用起来比较简单:另一种方法则是require An ...

  4. angularJS中directive与controller之间的通信

    当我们在angularJS中自定义了directive之后需要和controller进行通讯的时候,是怎么样进行通讯呢? 这里介绍3种angular自定义directive与controller通信的 ...

  5. AngularJS自定义Directive与controller的交互

    有时候,自定义的Directive中需要调用controller中的方法,即Directive与controller有一定的耦合度. 比如有如下的一个controller: app.controlle ...

  6. AngularJS之directive

    AngularJS之directive AngularJS是什么就不多舌了,这里简单介绍下directive.内容基本上是读书笔记,所以如果你看过<AngularJS up and runnin ...

  7. Angularjs之directive指令学习笔记(二)

    1.Directive的五个实例知道driective作用.其中字段restrict.template. replace.transclude.link用法 参考文章链接地址:http://damoq ...

  8. angularjs的directive详解

    Directive(指令)笔者认为是AngularJ非常强大而有有用的功能之一.它就相当于为我们写了公共的自定义DOM元素或CLASS属性或ATTR属性,并且它不只是单单如此,你还可以在它的基础上来操 ...

  9. 【angularJS】Directive指令

    AngularJS 通过被称为 指令 的新属性来扩展 HTML.指令是扩展的 HTML 属性,带有前缀 ng-. 内置指令 1.ng-app 指令初始化一个 AngularJS 应用程序. 定义了 A ...

随机推荐

  1. EXCEL VB

    全面控制 Excel首先创建 Excel 对象,使用ComObj:Dim ExcelID as Excel.ApplicationSet ExcelID as new Excel.Applicatio ...

  2. SPOJ 962 Intergalactic Map (从A到B再到C的路线)

    [题意]在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= 5 ...

  3. 分布式存储Memcache替代Session方案

    PHP自带的Session实际是在服务器中为每个客户建立独立的文件存放各自的信息. 在不做处理的情况下,很容易被客户端伪造.并且由于采用文件形式,所以存在着IO 读写的瓶颈.一般当用户在线达到1000 ...

  4. Scrum 体验活动笔记

    2014-03-10  Isoftstone library 1.识别角色(用户),形象 :名称.痛处.属性.需求 2.编写故事 story以验证需求是否正确:我们假设(客户)  进行验证结果... ...

  5. MyEclipse2014安装ADT插件(适用于其他版本)

    这次,本文采用公认的最佳插件安装方式——link方式来安装ADT插件,此方法适用于Eclipse以及MyEclipse其他版本.下面为大家一一道来: 大致过程如下: 官方的在线安装很麻烦,找了很久,终 ...

  6. HDU4612 Warm up 边双(重边)缩点+树的直径

    题意:一个连通无向图,问你增加一条边后,让原图桥边最少 分析:先边双缩点,因为连通,所以消环变树,每一个树边都是桥,现在让你增加一条边,让桥变少(即形成环) 所以我们选择一条树上最长的路径,连接两端, ...

  7. CentOS 6.4利用xampp安装bugfree3

    1.下载xampp 安装 http://www.apachefriends.org/zh_cn/xampp.html 直接执行.run文件安装  默认会安装到/opt/lampp 2 .启动xampp ...

  8. JSON 格式的转换: 数组、字符串、List集合、DataTable,实体对象

    JSON验证工具:http://jsonlint.com/JSON简明教程:http://www.w3school.com.cn/json/Newtonsoft.Json类库下载:http://jso ...

  9. 将UIWebView显示的内容转为图片和PDF

    今天开发MarkEditor时要用到将 UIWebView 中显示的内容转为图片,方便转发到各个社交网络(Twiiter,Facebook,Weibo),这样内容就不受长度限制,类似于长微博. 之前关 ...

  10. [2015更新]用Word2007写CSDN博客

    搞了半天终于可以用word2007发布CSDN博客了,特分享出来,以方便其他用户. 所示的界面.     图1 office按钮 所示的管理账号,然后点击"新建"也可以进入图3所示 ...