我必須錯過一些東西,但我不明白爲什麼這個指令沒有顯示出來,有沒有人可以幫忙?Angular指令不處理此代碼
<body ng-controller="MainCtrl">
<p>Test is <b>{{name}}</b> with myValue <b>{{myValue}}</b></p>
<my-new-directive my-test="{{myValue}}"></my-new-directive>
</body>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.myValue = true;
});
app.directive('myNewDirective', function() {
return {
restrict: 'E',
replace: true,
link: function(scope, element, attrs) {
attrs.$observe('myTest', function() {
scope.name = attrs.myTest;
if (!attrs.myTest) {
this.template = '<div>FALSE</div>';
} else {
this.template = '<div>TRUE</div>';
}
scope.$apply();
});
}
};
});
OKI冷靜,做工精細 –
現在,我試圖做templateUrl類似的東西,但myvalue的將是一個服務調用的結果。任何想法我怎麼能實現呢?需要一個角假人當然肯定:-( –
對於templateUrl你只是更換templateUrl屬性模板屬性:templateUrl:「template.html」 至於服務,一旦你有一個返回值的服務,並更新myValue作用於該範圍,模板會相應地執行。根本不需要更改指令實施。 –