AngularJs1.x自定义指令独立作用域的函数传入参数方法

 更新时间:2018年10月09日 09:52:46   作者:meloseven  
今天小编就为大家分享一篇AngularJs1.x自定义指令独立作用域的函数传入参数方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

在定义指令的scope属性如果设置成了{},那就成为了一个独立作用域,如果要传入一个方法,使用&,但是这里的传参有点不一样。

先看下官网解释:

& or &attr - provides a way to execute an expression in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value expression. Often it's desirable to pass data from the isolated scope via an expression and to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).

这里有个例子:

<!DOCTYPE html>
<html>
<head lang="en">
 <meta charset="UTF-8">
 <title></title>
</head>
<body ng-app="app1">
<div ng-controller="MyCtrl">
 <div ng-repeat="item in items" my-component isolated-expression-foo="updateItem(item,temp)">
 {{item|json}}
 </div>
</div>
</body>
<script src="../scripts/angular.js"></script>
<script>
 var myModule = angular.module('app1', [])
 .directive('myComponent', function () {
 return {
 restrict:'A',
 scope:{
 isolatedExpressionFoo:'&'
 },
 link:function(scope,element,attr) {
 scope.isolatedExpressionFoo();
 }
 };
 })
 .controller('MyCtrl', ['$scope', function ($scope) {
 $scope.items=[{id:1,value:"test"},{id:2,value:"TEst2"}];
 $scope.updateItem = function (item,temp) {
 console.log("Item param "+item.id);
 console.log("temp param " + temp);
 }
 }]);

</script>
</html>

以上这篇AngularJs1.x自定义指令独立作用域的函数传入参数方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 实践中学习AngularJS表单

    实践中学习AngularJS表单

    表单是最常用的一种组建。在Angular.js中,其实并没有单独的为表单添加多少特殊功能。但是,利用Angular.js框架本身的特点,可以更友好的呈现表单。下面将介绍几种常用的功能在Angular中是如何巧妙实现的
    2016-03-03
  • AngularJs用户输入动态模板XSS攻击示例详解

    AngularJs用户输入动态模板XSS攻击示例详解

    这篇文章主要给大家介绍了关于AngularJs用户输入动态模板XSS攻击的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用angularjs具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • 解决angular2 获取到的数据无法实时更新的问题

    解决angular2 获取到的数据无法实时更新的问题

    今天小编就为大家分享一篇解决angular2 获取到的数据无法实时更新的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Spartacus CMS Feature selector的实现解析

    Spartacus CMS Feature selector的实现解析

    这篇文章主要为大家介绍了Spartacus CMS Feature selector的实现解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • Angular.js实现注册系统的实例详解

    Angular.js实现注册系统的实例详解

    Angular.js是Google开发的前端技术框架,最近一直在学习Angular.js,通过对angular.js的简单理解后发现,angular.js通过一些简单的指令即可实现对DOM元素的操作,其特色为双向数据绑定,下面这篇文章给大家详细介绍Angular.js实现注册系统的方法,一起来看看吧。
    2016-12-12
  • 详解webpack+es6+angular1.x项目构建

    详解webpack+es6+angular1.x项目构建

    这篇文章主要介绍了详解webpack+es6+angular1.x项目构建, 小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • AngularJS语法详解(续)

    AngularJS语法详解(续)

    本文续上文,接着介绍AngularJS语法,和上文一样,都是通过示例来向大家分析说明,非常不错的一篇文章,推荐给大家。
    2015-01-01
  • 基于Angular中ng-controller父子级嵌套的相关属性详解

    基于Angular中ng-controller父子级嵌套的相关属性详解

    今天小编就为大家分享一篇基于Angular中ng-controller父子级嵌套的相关属性详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Angular和百度地图的结合实例代码

    Angular和百度地图的结合实例代码

    最近在angular的项目,但是我用直接引用百度地图的方法引进js,写html,js代码,发现报错,接下来通过本文给大家介绍angular和百度地图的结合,需要的朋友可以参考下
    2016-10-10
  • Angularjs在360兼容模式下取数据缓存问题的解决办法

    Angularjs在360兼容模式下取数据缓存问题的解决办法

    这篇文章主要为大家详细介绍了Angularjs在360兼容模式下取数据缓存问题的解决办法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06

最新评论