在 Directives/Controller 中绑定 $scope

bind $scope in Directives/Controller

将作用域绑定到我的 Directive/Controller 后,我不能再使用它自己的指令了,有什么我应该修复的吗?我试图找到答案,但到目前为止一无所获..

ANGULARJS:

return {
        scope: { fullpost:'@' },
        controller: function ($scope, $element) {

            // Edit Btn
            $scope.editbtn = "Edit";

$scope.editbtn 不再显示在 html 中。

HTML:

<full-post fullpost="fullpost">
<!-- edit tools -->
<div class='yardtools'>
<button id='edit' ng-click='edit()'><i class='fa fa-pencil-square-o'></i> {{ editbtn }}</button>
<button id='update' class='hidden'><i class='fa fa-wrench'></i> Update</button>
<button id='delete'ng-click='delete()'class='hidden'><i class='fa fa-trash-o'></i> Delete</button>
    </div>
<!-- post -->
<div class='yardman-post' data-post-id=" + id + ">
    <div ym='info'>
        <p>Post id: <em> {{ fullpost.id }}</em>, Posted on: <em>{{ fullpost.date }}</em>, by <em>AUTHOR</em>
        </p>
    </div>
    <div ym='title' contenteditable='false'>{{ fullpost.title }}</div>
    <div ym='body' contenteditable='false'>{{ fullpost.text }}</div>
</div>
</full-post>

我建议您将指令元素的内部 html 移动到某个模板,尽管可以选择使用 ng-transclude 但是查看当前的 html 您可以使用模板使用指令的内部内容并使用 templateUrl.

访问该模板

directiveTemplate.html

<div class='yardtools'>
<button id='edit' ng-click='edit()'><i class='fa fa-pencil-square-o'></i> {{ editbtn }}</button>
<button id='update' class='hidden'><i class='fa fa-wrench'></i> Update</button>
<button id='delete'ng-click='delete()'class='hidden'><i class='fa fa-trash-o'></i> Delete</button>
    </div>
<!-- post -->
<div class='yardman-post' data-post-id=" + id + ">
    <div ym='info'>
        <p>Post id: <em> {{ fullpost.id }}</em>, Posted on: <em>{{ fullpost.date }}</em>, by <em>AUTHOR</em>
        </p>
    </div>
    <div ym='title' contenteditable='false'>{{ fullpost.title }}</div>
    <div ym='body' contenteditable='false'>{{ fullpost.text }}</div>
</div>

指令

return {
    scope: { fullpost:'@' },
    templateUrl: 'directiveTemplate.html'
    controller: function ($scope, $element) {

        // Edit Btn
        $scope.editbtn = "Edit";
     }
 }