AngularJS:当嵌套元素时,Directive无权访问控制器

时间:2015-09-19 10:11:17

标签: javascript angularjs angular-ui

问题:为什么在某些网页上使用该元素时,我的简单指令是否可以访问其控制器?

其他信息指令在主页上正常工作。我无法使用我的指令的HTML使用ui-view调用,包含在下面。

<div ui-view></div>

指令

var postcardAppDirectives = angular.module('postcards.directives', ['postcards.controllers']);

postcardAppDirectives.directive('postcardLink', function () {
    return {
        restrict: "AE",
        template: '<div class="well">{{ ctrl.ownerObj.user }}</div>',
        controller: 'postcardLinkController as ctrl'
    }
});

控制器

var PostcardsControllers = angular.module('postcards.controllers', ['postcards.factories', 'postcards.services']);

PostcardsControllers.controller('postcardLinkController', [function ()
    this.ownerObj = {
        "user": 'test_owner',
        "pointofthis": 'inane testing'
    }
}]);

HTML(不起作用)

<div ng-controller="DashboardAccountController as infoCtrl" class="container full-container">

<!------------------ DIRECTIVE REFERENCE HERE -------->        
<postcard-link></postcard-link>

    <h1>
        <small>Welcome</small>
        <username-text></username-text>
    </h1>
    <img ng-show="infoCtrl.user.member_profile.profile_image" ng-src="{{ infoCtrl.user.member_profile.profile_image }}"
         style="min-height:100px;height:100px;" alt='profileImage'/>
    <button type="button" class="btn btn-link col-md-offset-2" ng-click="infoCtrl.loadPage()">Modify Account</button>
    <div class="row">
...

Not working here

HTML(工作正常)

<ng-include src="'static/app/carousel/_carousel.html'"></ng-include>

<postcard-link></postcard-link>
<div class="container">
    <section style="text-align: center">
        <h1>How It Works</h1>

Working just fine.

1 个答案:

答案 0 :(得分:0)

我看到你使用了不同的模块,这就是为什么一个模块不知道另一个模块指令的原因。你应该使用相同的模块。

像:

JS:

union

像往常一样使用html:

select i.InvName, i.InvSenderTotal, i.InvReceiverTotal, u.FullName
FROM Invites i INNER JOIN
     Users u
     ON u.UserID IN (i.InvReceiverID, i.InvSenderID)
WHERE i.InvStatus = 'Sent' AND i.InvSenderID = 2000;