说我有指令:
angular.module('myApp').directive('myDirective', function ($compile) {
return {
link: function ($scope, $element, $attrs) {
var $randomElem = $('<div>');
$element.append($compile($randomElem)($scope));
$randomElem.remove();
}
}
});
Run Code Online (Sandbox Code Playgroud)
范围会自动销毁吗?如果没有,我怎么能摧毁它?
当我需要从不同的范围内访问类属性(或方法)时,我必须将其分配给函数范围内的变量.
class MyClass {
constructor(API) {
this.API = API;
this.property = 'value';
}
myMethod() {
var myClass = this; // I have to assign the class to a local variable
this.API.makeHttpCall().then(function(valueFromServer) {
// accessing via local variable
myClass.property = valueFromServer;
});
}
}
Run Code Online (Sandbox Code Playgroud)
对于每种方法,我都不需要这样做.还有另一种方法吗?