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禁止img预览该怎么解决?2. javascript - vue+iview upload传参失败 跨域问题后台已经解决 仍然报403,这是怎么回事啊?3. python - django 按日归档统计订单求解4. objective-c - 从朋友圈跳到我的APP 如何实现?5. 百度地图api - Android 百度地图 集成了定位,导航 相互的jar包有冲突?6. 网页爬虫 - python爬虫用BeautifulSoup爬取<s>元素并写入字典,但某些div下没有这一元素,导致自动写入下一条,如何解决?7. javascript - vscode alt+shift+f 格式化js代码,通不过eslint的代码风格检查怎么办。。。8. 请教一个python字符串处理的问题?9. sql语句 - mysql中关联表查询问题10. 怎么可以实现在手机浏览器看到链接的title属性,就是鼠标放上去会有一个tip效果的
