我有一个看起来像这样的指令
<a href ng-click="metric='Distance'; metricshow=false;" > <a href ng-click="metric='Time'; metricshow=false;">现在在我的控制器中。
console.log($scope.metric); switch($scope.metric) { case 'Distance' : console.log('Distance'); break; case 'Time' : console.log('Time'); break; }console.log()给我未定义。 为什么?
I have a directive which looks something like this
<a href ng-click="metric='Distance'; metricshow=false;" > <a href ng-click="metric='Time'; metricshow=false;">Now In my controller.
console.log($scope.metric); switch($scope.metric) { case 'Distance' : console.log('Distance'); break; case 'Time' : console.log('Time'); break; }The console.log() gives me undefined. Why?
最满意答案
当你的控制器第一次运行时,$ scope.metric变量没有被创建,也没有被分配给任何东西。 你也可能不喜欢在你的视图中使用逻辑,而是使用函数。
指示:
<a href ng-click="setMetric('Distance')" > <a href ng-click="setMetric('Time')"> <a href ng-click="getMetric()">现在在您的控制器中:
$scope.setMetric = function (type) { $scope.metric = type; } $scope.getMetric = function () { console.log($scope.metric); }工作Plunker与一些补充
When your controller first run, the $scope.metric variable was not created nor assigned to anything. You may also prefer not to use logic in your view, use functions instead.
Directive:
<a href ng-click="setMetric('Distance')" > <a href ng-click="setMetric('Time')"> <a href ng-click="getMetric()">Now In your controller:
$scope.setMetric = function (type) { $scope.metric = type; } $scope.getMetric = function () { console.log($scope.metric); }Working Plunker with some additions
如何在我的控制器中使用ng-click变量?(How to use ng-click variables in my controller?)我有一个看起来像这样的指令
<a href ng-click="metric='Distance'; metricshow=false;" > <a href ng-click="metric='Time'; metricshow=false;">现在在我的控制器中。
console.log($scope.metric); switch($scope.metric) { case 'Distance' : console.log('Distance'); break; case 'Time' : console.log('Time'); break; }console.log()给我未定义。 为什么?
I have a directive which looks something like this
<a href ng-click="metric='Distance'; metricshow=false;" > <a href ng-click="metric='Time'; metricshow=false;">Now In my controller.
console.log($scope.metric); switch($scope.metric) { case 'Distance' : console.log('Distance'); break; case 'Time' : console.log('Time'); break; }The console.log() gives me undefined. Why?
最满意答案
当你的控制器第一次运行时,$ scope.metric变量没有被创建,也没有被分配给任何东西。 你也可能不喜欢在你的视图中使用逻辑,而是使用函数。
指示:
<a href ng-click="setMetric('Distance')" > <a href ng-click="setMetric('Time')"> <a href ng-click="getMetric()">现在在您的控制器中:
$scope.setMetric = function (type) { $scope.metric = type; } $scope.getMetric = function () { console.log($scope.metric); }工作Plunker与一些补充
When your controller first run, the $scope.metric variable was not created nor assigned to anything. You may also prefer not to use logic in your view, use functions instead.
Directive:
<a href ng-click="setMetric('Distance')" > <a href ng-click="setMetric('Time')"> <a href ng-click="getMetric()">Now In your controller:
$scope.setMetric = function (type) { $scope.metric = type; } $scope.getMetric = function () { console.log($scope.metric); }Working Plunker with some additions
发布评论