簡體   English   中英

如何從`directive`控制器更新`$ scope`對象

[英]How to update the `$scope` object from `directive` controller

我試圖從控制器更新$scope對象,但它沒有更新。 但在控制台我正在獲得更新狀態。

這是我的代碼:

var galleryMenu = ['$route', function ($route) {

    return {

        scope : true,

        replace : true,

        template : function () {

            var page = $route.current.className || 'home';

            return galleryMenuItem(page);

        },

        controller : function ($scope, $element) {

            $scope.galleryProject = function () {

                $scope.galleryShow = !$scope.galleryShow;
                console.log($scope.galleryShow) //gives me true, but `DOM` not updating.

            }


        }

    }

}];

您的指令使用scope: true表示您已創建一個原型繼承自父作用域的作用域。

然后您的galleryShow對象應遵循dot rule

$scope.model = {};
$scope.model.galleryShow = 'something';

那么在你的指令中你可以通過原型繼承得到$scope.model對象,你可以通過$scope.model.galleryShow更改父范圍。

指令控制器

controller : function ($scope, $element) {
     $scope.galleryProject = function () {
         $scope.model.galleryShow = !$scope.galleryShow;
         console.log($scope.model.galleryShow) //gives me true, but `DOM` not updating.
     }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM