第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

指令之間的AngularJS通信

指令之間的AngularJS通信

繁花如伊 2019-11-14 10:18:40
我是Angular.js的新手,我的應(yīng)用程序需要指令之間的某些通信,我閱讀了一些有關(guān)鏈接和需求的文檔,但無(wú)法確切了解其工作原理。對(duì)于一個(gè)簡(jiǎn)單的例子,我有:live小提琴:http : //jsfiddle.net/yw235n98/5/2個(gè)指令:firstDir,secondDir ::帶有一些數(shù)據(jù)firstDir具有單擊功能,它將更改數(shù)據(jù)值當(dāng)firsDir單擊功能被觸發(fā)時(shí),我也想在secondDir中更改數(shù)據(jù)。HTML:<body ng-app="myApp">First Directive :   <first-dir >    <h3>{{firstCtrl.data}}</h3>    <button ng-click="firstCtrl.set('NEW VALUE')">Change Value</button></first-dir>Second Directive : <second-dir>    <h3>{{secondCtrl.data}}</h3></second-dir>Javascript:(function(){    var app = angular.module('myApp', []);    app.directive("firstDir", function(){        return {            restrict : 'E',            controller : function(){                        this.data = 'init value';                this.set = function(value){                    this.data = value;                    // communication with second Directive ???                }                   },            controllerAs : 'firstCtrl'        };      });    app.directive("secondDir", function(){        return {            restrict : 'E',            controller : function(){                        this.data = 'init value';               },            controllerAs : 'secondCtrl'        };      });})();
查看完整描述

3 回答

?
弒天下

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個(gè)贊

在您的示例中,指令結(jié)構(gòu)不是父子結(jié)構(gòu)。因此,您無(wú)法通過(guò)其控制器共享方法。我會(huì)用$rootScope.$broadcast。(請(qǐng)參閱DOCS)


一個(gè)指令調(diào)用:


$rootScope.$broadcast('someEvent', [1,2,3]);

第二條指令偵聽(tīng):


 scope.$on('someEvent', function(event, mass) {

    console.log(mass)}

  );


固定指令:


app.directive("firstDir", function ($rootScope) {

    return {

        restrict: 'E',

        link: function (scope, element, attrs) {

            scope.dataToPass = 'empty';

            scope.doClick = function (valueToPass) {

                scope.dataToPass = valueToPass;

                $rootScope.$broadcast('someEvent', {

                    data: valueToPass

                });

            }

        }

    };

});


app.directive("secondDir", function () {

    return {

        restrict: 'E',

        link: function (scope, element, attrs) {

            scope.receivedData = 'none';


            scope.$on('someEvent', function (event, result) {

                scope.receivedData = result.data;

            });

        }

    }

});


查看完整回答
反對(duì) 回復(fù) 2019-11-14
?
飲歌長(zhǎng)嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

我正在使用的是導(dǎo)出指令控制器。假設(shè)我有以下指令:


app.directive('mainDirective', function () {

  return {

    require: 'mainDirective'

    restrict: 'E',

    scope: {

      controller: '='

    },

    controller: [

      '$scope',

      function ($scope) {

        // controller methods

        this.doSomething = function () { ... },


        $scope.controller = this

        return this

      }

    ],

    link: function (scope, element, attrs, mainDirective) {

      // some linking stuff

    }

  }

});

我的html看起來(lái)像這樣:


<main-directive controller="mainDirective"></main-directive>

<sub-directive main-directive="mainDirective"></sub-directive>

如果我想從子指令控制主指令,我可以輕松地從它的作用域中獲取它并做我想做的任何事情...


app.directive('subDirective', function () {

  return {

    restrict: 'E',

    scope: {

      mainDirective: '='

    }

    link: function (scope, element, attrs) {

      // do something with main directive

      scope.mainDirective.doSomething();

    }

  }

});


查看完整回答
反對(duì) 回復(fù) 2019-11-14
  • 3 回答
  • 0 關(guān)注
  • 674 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)