angular.js - 兩個direcitve如何獲取值
問題描述
兩個不同的DIRECTIVE如何取對應scope中的值JS代碼
.directive(’save’,function(){ return{restrict:’EAC’,template:’<p id='btnSave'><img src='http://www.aoyou183.cn/wenda/images/savePic.png'></p>’,scope:{ goodsName: ’@goodName’},link:function(scope,element,attrs){ var childElem = element.find(’toggleName’); var childScope = childElem.isolateScope(); element.on(’click’, function() { var jsonData = scope.goodName; alert(jsonData);});} };});
.directive(’toggleName’, function() {return { restrict: ’ECA’, templateUrl: ’views/partials/toggleName.html’, transclute: true, link: function(scope, element, attrs) {scope.toggleName = function() { scope.isSuccessName = !scope.isSuccessName;}; }}; })
HTML代碼
<p class='realInputCon fr'><input type='text' maxlength='255' placeholder='請輸入' ng-model='goodName'> </p>
save取togglename中的goodName
問題解答
回答1:使用require就搞定了
.directive(’toggleName’, function() {return { restrict: ’ECA’, templateUrl: ’views/partials/toggleName.html’, controller: function($scope) {$scope.goodName = ’togglename’;this.getName = function() { return $scope.goodName;}; }, transclute: true, link: function(scope, element, attrs) {scope.toggleName = function() { scope.isSuccessName = !scope.isSuccessName;}; }}; }).directive(’save’,function(){ return{restrict:’EAC’,template:’<p id='btnSave'><img src='http://www.aoyou183.cn/wenda/images/savePic.png'></p>’,require: ’toggleNmae’,link:function(scope,element,attrs, toggleNameController){ var childElem = element.find(’toggleName’); var childScope = childElem.isolateScope(); element.on(’click’, function() { var jsonData = scope.goodName; alert(jsonData);}); //這里就是toggleName控制器的使用了 toggleNameController.getName();} };});
相關文章:
1. javascript - npm下載的模塊不完整是什么問題?2. java - Spring事務回滾問題3. apache - 本地搭建wordpress權限問題4. c++ - 如何在python的阻塞的函數中獲取變量值5. node.js - 我想讓最后進入數據庫的數據,在前臺最先展示,如何做到?6. wordpress - Nginx中禁止訪問txt,robots.txt文件例外,規則該怎么寫?7. 剛放到服務器的項目出現這中錯誤,有高手指點嗎8. python - django 按日歸檔統計訂單求解9. python 操作mysql如何經量防止自己的程序在之后被惡意注入(說白了就是問一下python防注入的一些要點)10. mysql - 面試題:如何把login_log表轉換成last_login表?
