angular.js - 两个direcitve如何获取值
问题描述
两个不同的DIRECTIVE如何取对应scope中的值JS代码
.directive(’save’,function(){ return{restrict:’EAC’,template:’<p id='btnSave'><img src='https://www.haobala.com/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='https://www.haobala.com/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. html5 - bootstrap修改样式的问题2. javascript - npm下载的模块不完整是什么问题?3. javascript - electron中的a标签怎么用浏览器直接打开而不是在框架窗体内4. 为啥获取不到呀?5. window下mysql中文乱码怎么解决??6. 前端 - 谁来解释下这两个 CSS selector 区别7. mysql每隔10来秒就有一次7、8MB的写入8. html5 - 最近在自学react 求一个react表单提交的例子9. javascript - vscode alt+shift+f 格式化js代码,通不过eslint的代码风格检查怎么办。。。10. javascript - ie11以下单击打开不了file,双击可以。求解?
