Javascript 有棱角的从html调用指令中的函数
我正试图发出指令。这样我就可以在html页面中使用它,如下面的代码片段所示:Javascript 有棱角的从html调用指令中的函数,javascript,html,angularjs,angularjs-directive,angularjs-scope,Javascript,Html,Angularjs,Angularjs Directive,Angularjs Scope,我正试图发出指令。这样我就可以在html页面中使用它,如下面的代码片段所示: <testview></testview> 现在唯一要做的就是渲染一个链接 <a><span ng-onmousedown='onSelected(\"creame\",event)'>{{value}}</span></a> {{value} 当我单击span时,我希望指令通过调用onSelected函数来更改$scope.value。
<testview></testview>
现在唯一要做的就是渲染一个链接
<a><span ng-onmousedown='onSelected(\"creame\",event)'>{{value}}</span></a>
{{value}
当我单击span时,我希望指令通过调用onSelected函数来更改$scope.value。但我不能这样说
完整代码示例
function testView() {
$scope.value =
{
name: "dummy"
}
testHtml += "<a><span ng-onmousedown='onSelected(\"creame\",event)' >" + $scope.value.name + "</span></a>";
return {
restrict: 'AE',
replace: 'true',
template: testHtml,
};
}
app.directive('testview', testView);
function onSelected(input)
{
$scope.value.name = input;
}
函数testView(){
$scope.value=
{
名称:“假人”
}
testHtml++=“++$scope.value.name+”;
返回{
限制:“AE”,
替换为:“true”,
模板:testHtml,
};
}
应用指令(“testview”,testview);
功能选择(输入)
{
$scope.value.name=输入;
}
有人知道我怎么做吗?
感谢您抽出时间试试这个
function testView() {
testHtml += "<a><span ng-onmousedown='onSelected(\"creame\")' >{{value.name }}</span></a>";
return {
restrict: 'AE',
replace: 'true',
template: testHtml,
};
}
app.directive('testview', testView);
app.controller('CampaignsOverviewController', Controller);
function Controller ($scope) {
$scope.onSelected = onSelected;
$scope.value =
{
name: "dummy"
}
function onSelected(input)
{
$scope.value.name = input;
}
}
函数testView(){
testHtml+=“{{value.name}}”;
返回{
限制:“AE”,
替换为:“true”,
模板:testHtml,
};
}
应用指令(“testview”,testview);
应用控制器(“活动服务控制器”,控制器);
功能控制器($scope){
$scope.onSelected=onSelected;
$scope.value=
{
名称:“假人”
}
功能选择(输入)
{
$scope.value.name=输入;
}
}
我认为在执行ngOnMouseDown指令之前,您需要修改模板。但是我不能调用它。为什么不呢?如果您像调用其他常规函数一样调用它会发生什么?您必须将该函数添加到$scope@takendarkk没有,没有错误。我认为这是ng mousedownim试图将函数保留在directiveinside指令对象中,添加属性控制器,并使用为控制器名称指定的值分配字符串。我在匆忙中使用了activatesovervicewcontroller
,将其更改为更有用的方法您必须提供一个控制器来绑定这些方法