angular.js - 兩個direcitve如何獲取值
問題描述
兩個不同的DIRECTIVE如何取對應scope中的值JS代碼
.directive(’save’,function(){ return{restrict:’EAC’,template:’<p id='btnSave'><img src='http://m.4tl426be.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://m.4tl426be.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. java - 是否類 類型指針、引用作為形參 ,函數結束不會自動析構類?2. 在mac下出現了兩個docker環境3. javascript - 這段代碼如何理解?4. javascript - history.replaceState()無法改變query參數5. angular.js - angular ng-class里面的引號問題6. matplotlib - python函數的問題7. mysql無法添加外鍵8. javascript - es6將類數組轉化成數組的問題9. javascript - react 中綁定事件和阻止事件冒泡10. javascript - 有沒有什么好的圖片懶加載的插件,需要包含監聽頁面滾動高度,然后再加載的功能
