我有一个md-select,里面有一个md-option。在md-option标签中,我有一个ng-click指向我定义的函数。问题是,当我从列表中单击一个选项时,它似乎永远不会进入我的功能。
这是我的HTML:
<div class="panel-body">
<md-select placeholder="{{getIntervalName()}}" ng-model="intervalSelected">
<md-option ng-repeat="item in intervals" value="{{item.name}}" ng-click="changeInterval(item)">
{{item.name}}
</md-option>
</md-select>
<div style="overflow: auto;">
<div id="parentDivChart" style="{{changeWidth()}};height:400px">
<canvas id="bar" class="chart chart-bar" chart-data="datta"
chart-labels="labels" chart-options="myoptions" chart-legend="legend">
</canvas>
<div tc-chartjs-legend chart-legend="legend"></div>
</div>
</div>
</div>
这是我在控制器中定义的函数:
function changeInterval(item) {
console.log("change intervaaaalll");
$scope.default_interval ="day";
intrvl = item.value;
setItem("interval", item.value);
$scope.loadChart();
};
它不会向控制台写任何东西,这意味着它永远不会到达我的功能。有人知道为什么吗?
提前致谢!
答案 0 :(得分:0)
在控制器中写下"$scope.changeInterval=changeInterval;"
或
使用"$scope.changeInterval=function (item) {...};"
答案 1 :(得分:0)
如果要在作用域上使用changeInterval方法,则必须将其添加到作用域中,如下所示:
在控制器中:
$scope.changeInterval(item) {
console.log("change intervaaaalll");
$scope.default_interval ="day";
intrvl = item.value;
setItem("interval", item.value);
$scope.loadChart();
};
不要忘记将$ scope作为控制器的依赖项。
E.g:
app.controller('myCtrl', function($scope) {
});
现在,当changeInterval函数被分配给作用域上的变量时,可以在视图上调用它。
答案 2 :(得分:0)
由于changeInterval()是控制器的私有函数,因此您无法访问它。您需要将其公开(将其绑定到控制器的变量)。
你应该采用这种方法:
$scope.changeInterval = changeIntervalapproach;
如果您正在使用带有controllerAs的视图模型方法,请使用:
vm.changeInterval = changeInterval;
答案 3 :(得分:0)
如果您提供了创建控制器的代码,那会更好。
但是,我认为您应该以这种方式创建方法changeInterval(item)
:
var nameOfYourApp = angular.module(‘nameOfYourApp’, []);
nameOfYourApp.controller('nameOfYourController', function($scope) {
$scope.changeInterval = function(item) {
console.log("change intervaaaalll");
$scope.default_interval ="day";
intrvl = item.value;
setItem("interval", item.value);
$scope.loadChart();
}
});
希望这有帮助!