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. dockerfile - 为什么docker容器启动不了?2. javascript - 手机网页如何,插入地图 ;并设置多个标注点 ,还可路线查询3. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””4. Vue项目打包后发布到github pages上,如何访问?5. thinkphp6中怎么把类放到容器中?6. python - 如何使用websocket在网页上动态示实时数据的折线图?7. javascript - arguments.callee8. 怎么学好php9. angular.js - angularjs directive怎么实现通过点击事件更换模版?10. javascript - Ajax加载Json时,移动端页面向左上角缩小一截儿,加载完成后才正常显示,这该如何解决?

网公网安备