美文网首页
angularjs中的controller值传递,$emit,$

angularjs中的controller值传递,$emit,$

作者: 听风就是雨之路人甲 | 来源:发表于2016-11-17 13:11 被阅读0次

概念

  1. $emit,$broadcast设置值,$on获取值
  2. $emit向上传递,$broadcast向下传递
EXAMPLE
  • html
    <pre>
    <div ng-controller="ParentController">
    parentCount:{{ count }}
    <div ng-controller="CurentController">
    <button type="button" ng-click="emit()">emit</button>
    <button type="button" ng-click="broadcast()">broadcast</button>
    curentCount:{{ count }}
    <div ng-controller="ChildController">
    childCount:{{ count }}
    </div>
    </div>
    <div ng-controller="BorController">
    borCount:{{ count }}
    </div>
    </div>
    </pre>
  • js
    <pre>
    // 父级controller
    .controller('ParentController', function($scope) {
    $scope.count = 0;
    $scope.$on('p-count', function(d, data){
    console.log(data); // emit中保存的值
    $scope.count ++;
    })
    })
    // 使用$emit,$broadcast保存值,可以在父级,子级和本身中取得
    .controller('CurentController', function($scope) {
    $scope.count = 0;
    $scope.emit = function(){
    $scope.count ++;
    // 向父级controller传递值p-count
    $scope.$emit('p-count', $scope.count);
    }
    $scope.broadcast = function(){
    $scope.count = $scope.count + 1;
    // 可以不传第二个args,$on获取时,第二个参数为undefined
    $scope.$broadcast('c-count');
    }
    })
    .controller('ChildController', function($scope){
    $scope.count = 0;
    $scope.$on('c-count', function(d,data){
    console.log(data); // undefined
    $scope.count ++
    })
    })
    // 兄弟controller,不会被影响
    .controller('BorController', function($scope) {
    $scope.count = 0;
    // 下面两个不被触发,回调函数不执行,不输出任何东西
    $scope.$on('p-count', function(d,data){
    console.log(data);
    })
    $scope.$on('c-count', function(d,data){
    console.log(data);
    }
    })
    </pre>

相关文章

  • angularjs中的controller值传递,$emit,$

    概念 $emit,$broadcast设置值,$on获取值 $emit向上传递,$broadcast向下传递 EX...

  • angular中$broadcast、$emit和$on

    angular中$broadcast、$emit和$on $emit只能向parent controller传递e...

  • vue组件通信

    通常父子组件通信都是用props和$emit进行传递,父组件通过props传值给子组件,子组件通过$emit传值给...

  • angularjs 服务(service)

    在angularjs中,服务是一个函数或者对象,使用它,需要在controller中定义。angularjs中有3...

  • vue中的.sync修饰符

    产生缘由 vue中组件间的传值为单向传递,子组件想要更新父组件的值,需要用emit触发父组件函数,用.sync修饰...

  • vue父子组件之间传值

    父组件向子组件传值是通过属性(props)的形式进行传递子组件向父组件传值是通过事件($emit)的形式进行传递....

  • angularjs $emit $brodcast $on 事件

    Angularjs中不同作用域之间可以通过组合使用$broadcast,$emit,$on的事件广播机制来进行通信...

  • vue子组件给父组件传属性

    1.普通属性传递方式 子组件通过this.$emit('update:属性名',属性值)修改父组件的值 1.组件内...

  • vue---子组件给父组件传值

    子组件: 子组件通过this.$emit()的方式将值传递给父组件 注意:这里的func是父组件中绑定的函数名 父组件:

  • vue子组件给父组件传值

    子组件 子组件通过this.$emit()的方式将值传递给父组件 注意:这里的func是父组件中绑定的函数名父组件

网友评论

      本文标题:angularjs中的controller值传递,$emit,$

      本文链接:https://www.haomeiwen.com/subject/ujudpttx.html