我正在写一个指令。
这是我正在使用的代码:
angular.module('weather', []).directive('myWeather', [
'$ionicSideMenuDelegate', function($ionicSideMenuDelegate) {
var createWeatherConditionLabel, linkFunction;
createWeatherConditionLabel = function(type) {
var label;
label = '';
debugger;
switch (type) {
case 0:
label = 'Clear';
break;
case 1:
label = 'Clear';
break;
case 2:
label = 'Light Cloud';
break;
case 3:
label = 'Light Cloud';
break;
case 5:
label = 'Windy';
break;
case 6:
label = 'Foggy';
break;
case 7:
label = 'Cloudy';
break;
case 15:
label = 'Rain';
break;
case 18:
label = 'Sleet';
break;
case 21:
label = 'Hail';
break;
case 27:
label = 'Snow';
break;
case 30:
label = 'Showers';
}
return label;
};
linkFunction = function(scope, el, attr) {
console.log("scope:", scope);
scope.showWeatherDetails = false;
scope.evtClickExpandMenuWeather = function() {
if (scope.showWeatherDetails === false) {
return scope.showWeatherDetails = true;
} else {
return scope.showWeatherDetails = false;
}
};
scope.$watch((function() {
return $ionicSideMenuDelegate.isOpenRight();
}), function(isOpen) {
if (!isOpen) {
return scope.showWeatherDetails = false;
}
});
return scope.weatherLabel = createWeatherConditionLabel(scope.weatherType);
};
return {
restrict: 'E',
replace: true,
templateUrl: './directives/tpl.html',
scope: {
weatherData: "=",
weatherType: "="
},
link: linkFunction
};
}
]);
该指令的调用如下:
<my-weather weather-data="zones[0].weatherData" weather-type="zones[0].weatherData.weather_type"></my-weather>
我需要调用一个函数来weatherLabel
在指令模板中创建和使用它,但我不能,因为 scope.weatherType 是未定义的。
如何在调用链接函数之前等待定义范围?
或者,是否有更好、更有效(性能方面)的方法来做到这一点?非常感谢您的帮助