从指令调用控制器

时间:2016-01-18 10:12:29

标签: angularjs angularjs-directive controller

我试图从指令调用一个控制器....这是我正在编写的代码

penApp.directive('enpo', function() {
	return {
   	restrict: 'E',
	
    scope: {
    	 info: '=',
		 dragEvent: '&dragParent'
	},
	templateUrl: 'enpo.html',
	link: function(scope, element, attrs){
		
		var circleDiv = element.find(".circle")
		element.droppable({
			
			
		})
		element.draggable({
			handle: ".circle",
			drag: function( event, ui ) {
				
				scope.dragEvent();
			}
		});
		var eDiv = element.find(".temp")
		$(eDiv).draggable({revert: true});
	}
};

})

这是.html文件的代码

<div class="row ">
	<div id="n_div" class="col-xs-12 dd_area">
		<end-point drag-parent="drawLine()" ng-repeat="info in stageObjectArray" info="info"></end-point>
	</div>
 </div>

这是我在控制器中编写的函数

$scope.drawLine = function(){
	
		console.log("Called thsssssse function")
	}

我无法弄清楚这里出了什么问题...任何人都可以指导......控制器功能没有被调用

1 个答案:

答案 0 :(得分:0)

您能否更清楚一下每段代码是什么?

希望这会帮助你:

在我看来,你已经创建了一个带有链接功能和html模板的指令。

在HTML模板中,您尝试在已编写的控制器中调用该函数,但是我没有看到您将控制器附加到要使用它的HTML中。

尝试将ng-controller =“MyController”(MyController是包含$ scope.drawline函数的控制器名称)添加到HTML中,如下所示:

<div class="row ">
    <div id="n_div" class="col-xs-12 dd_area" ng-controller="MyController">
        <end-point drag-parent="drawLine()" ng-repeat="info in stageObjectArray" info="info"></end-point>
    </div>
 </div>

顺便说一句,我看不到你在HTML中实际使用'enpo'指令的位置。我假设你实际上是在调用它,但是在这里省略了那部分代码。

相关问题