我已經定義我的角應用內以下指令:Angular指令「=」值綁定到範圍,但未定義?
(function() {
'use strict';
angular
.module('almonds')
.directive('security', ['$animate', 'AuthFactory', directive]);
function directive($animate, AuthFactory) {
var directive = {
restrict: 'EA',
scope: {
operation: "@",
clearance: "@",
project: "="
},
link: linkFunc
};
return directive;
function linkFunc($scope, $element, $attr, ctrl, $transclude) {
var block, childScope, previousElements;
console.log($scope.project);
if($scope.project) {
var projectId = $scope.project.id;
}
var operation = $scope.operation;
var clearance = $scope.clearance;
if (projectId) {
var value = AuthFactory.hasProjectAccess(projectId, clearance);
console.log('PROJECT SECURITY:', projectId, clearance, value);
} else {
var value = AuthFactory.hasAccess(operation, clearance);
console.log('ORG SECURITY:', operation, clearance, value);
}
}
}
// Controller.$inject = ['$scope'];
//
// function Controller($scope) {
// var vm = this;
//
// activate();
//
// function activate() {
//
// }
// }
})();
應當用作接收一個operation
或project
值以及一個clearance
值,這將隨後被用於是否所述的元件元素將呈現(我省略了那部分,但它的功能與ng-if
基本相同)。
下面是使用它的一個例子:
<span security project="vm.project" clearance="admin">
<a role="button" ng-click="vm.confirmDeletion();"><span class="melon-icon-md melon-icon-trash"></span></a>
</span>
發生了什麼事雖然是,即使vm.project
確實定義,即console.log($scope.project);
產生undefined
。有趣的是,如果我只是console.log($scope);
它將包含一個project
財產與我需要的信息。我究竟做錯了什麼?
我其實只需要在項目的id
價值,所以我可以在指令中通過在整個project
對象並訪問其id
,或以某種方式單獨通過id
數。
'<角色= 「按鈕」'...什麼是錯的'
我使用'ui-sref',而不是在特定的鏈接上,但爲了保持一致性,它們都是一樣的。 – Tiago
請給我們提供plunker或jsfiddle來查看原因 –