gpt4 book ai didi

AngularJS : How to change scope value from isolated scope within directive

转载 作者:行者123 更新时间:2023-12-02 23:25:19 24 4
gpt4 key购买 nike

如何更改指令的范围值?我已经尝试过这种方式:

在模板中:

<h1>{{abcd}}</h1>
<example-directive abcd="abcd"></example-directive>

在指令中

..
scope: {
abcd: '='
},
link: function($scope){
$scope.abcd = "change it pleeease";
}
....

但结果我什么也没得到。所以“h1”标签是空的......有人知道为什么吗?

update1 - 我的完整代码

指令:

(function(){
'use strict';

var standingsDirective = function(api, $http){
return {
restrict: 'E',
scope : {
sid: '=',
loadingstatus: '='
},
templateUrl: "teams/views/standings.html",
link: function(scope){
scope.loadingstatus = "loading";

$http.get(api+'/getsomething'+scope.id).success(function(result){
scope.data = result;

if(scope.data && scope.data.length > 0){
scope.loadingstatus = "loaded";
}else{
scope.loadingstatus = "notloaded";
}
}).error(function(){
scope.loadingstatus = "notloaded";
});
}
};
};

var teamsModule = angular.module('app.teams');
teamsModule.directive('standings', ['api', '$http', standingsDirective]);

}());

模板文件:

...
<a ng-show="loadingstatus == 'loaded'" ng-click="subview='standing'" class="activeLink">standings - {{loadingstatus}}</a>
...
<standings sid="sid" loadingstatus="loadingstatus" ng-show="subview=='standing'"></standings>
...

最佳答案

回答我的问题:

首先,非常感谢“Fedaykin”帮助我提出评论。他是那个指出它应该有效的人,因此问题应该出在我代码的另一部分。

发现 ng-if 属性阻止了我的指令的执行。html 代码如下所示:

<a ng-show="loadingstatus == 'loaded'" ng-click="subview='standing'" class="activeLink">standings - {{loadingstatus}}</a>
<div ng-if="subview=='standing'">
...
<standings sid="sid" loadingstatus="loadingstatus" ng-show=" subview=='standing'"> </standings>
....
</div>

所以这导致了这种奇怪的行为。我希望这篇文章能够帮助其他有类似问题的人解决问题。

关于AngularJS : How to change scope value from isolated scope within directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27806699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com