如何在angularjs中写一个指令
发布时间:2020-12-17 07:33:24 所属栏目:安全 来源:网络整理
导读:我喜欢使用指令来制作一个自定义组件.我检查了很多教程,它让我感到困惑,任何人都可以解释一个指令的工作原理.我打算制作的组件是 shout-list/shout-list 喊叫列表的模板将是这样的 div class="shout" ng-repeat="shout in shouts" p{{shout.message}}/p img
我喜欢使用指令来制作一个自定义组件.我检查了很多教程,它让我感到困惑,任何人都可以解释一个指令的工作原理.我打算制作的组件是
<shout-list></shout-list> 喊叫列表的模板将是这样的 <div class="shout" ng-repeat="shout in shouts"> <p>{{shout.message}}</p> <img src="media/images/delete.png" width="32" height="32" ng-click="deleteShout({{$index}},'{{shout._id}}')"/> </div>
这是您的指令,有一些内联评论:
angular.module( 'directives',[] ).directive( 'shoutList',function () { return { restrict: 'E',// allow as an element; the default is only an attribute scope: { // create an isolate scope shouts: '=' // map the var in the shouts attribute to this scope },templateUrl: 'templates/shoutList.html',// load the template file controller: function ( $scope ) { // we declare a your function for use in the view $scope.deleteShout = function ( idx,id ) { // do whatever }; } }; }); 和模板文件: <div class="shout" ng-repeat="shout in shouts"> <p>{{shout.message}}</p> <img src="media/images/delete.png" width="32" height="32" ng-click="deleteShout({{$index}},'{{shout._id}}')" /> </div> 现在你可以在你的代码中使用它,就像这样: 控制器: .controller( 'MainCtrl',function ( $scope ) { $scope.myShouts = // ... }); 视图: <shout-list shouts="myShouts"></shout-list> 希望这可以帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 第四章 Shell 流程控制
- events – bootstrap如何检测哪个按钮关闭’hidden_??modal
- Bootstrap入门之栅格布局
- angularjs – Grunt Concat任务:如何排除某些文件?
- WebService 中操作 HttpRequest / HttpResponse (一)
- 禁用侧面菜单的滑动以打开Ionic 2中的登录页面(或任何页面)
- 如何绑定到angularjs指令中的自定义事件?
- angular – ionic:添加到app.module.ts时FileUploadOption
- scala – 从线程模型转换为actor
- 量角器 – toBe(真值)和toBeTruthy()之间的区别