【问题标题】:unable to update controller model value from custom directive with controller as syntax无法使用控制器作为语法从自定义指令更新控制器模型值
【发布时间】:2015-06-01 16:36:09
【问题描述】:

我在使用控制器作为语法从自定义指令更新我的模型值时遇到问题。我可以通过 console.log 清楚地看到指令中的值正在发生变化,但是它们没有在控制器中得到更新。 <h4>{{self.tabs}} </h4> 始终显示相同的值。

此外,我想在控制器(self --> tabs)模型上进行监视以添加“活动类”,但是当我尝试使用监视时,它会抛出错误。所以我评论了那部分。以下是手表相关代码,不工作:

$scope.$watch('tabsCtrlself.tabs.index', function() {
          if (tabsCtrlself.tabs.index === index) {
            angular.element($element).addClass('active');
          }
        });

请找到我的plunker,谁能指导我解决这个问题?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope angularjs-controller


    【解决方案1】:
    you can use 'ng-class' attribute,eg:
    <en-tab data-pane="myPaneName1" ng-class="{active:index==1}">Tab #1</en-tab>
    <en-tab data-pane="myPaneName2" ng-class="{active:index==2}" >Tab #2</en-tab>
    <en-tab data-pane="myPaneName3"  ng-class="{active:index==3}">Tab #3</en-tab>
    'index' is a object in angular: $scope.index, initializes its value:
     $scope.index=1;
    The first item is selected by default.
    
    I think the object of 'tabsCtrl.tabs.index' is not a angular object, so can't auto refresh
    

    【讨论】:

    • 没关系。但我正在寻找的是了解指令的内部结构并从指令中为所选选项卡添加'addClass('active')'。为此,我们甚至可以使用 'data-selected="true" '。但我需要从指令中添加活动类。
    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2017-02-07
    • 2023-03-03
    • 1970-01-01
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多