【问题标题】:Update value in custom directive from controller从控制器更新自定义指令中的值
【发布时间】:2016-09-23 04:00:01
【问题描述】:

我似乎无法正常工作。我是 angularjs 的新手,我正在将现有的网络应用程序转换为使用 angularjs。 我环顾四周,似乎找不到答案。

这是主要的html:

<div>
   <custom-header form-object="mainCtrl.form"></custom-header>
   <custom-sidebar form-object="mainCtrl.form"></custom-sidebar>
   <custom-subheader form-object="mainCtrl.form"></custom-subheader>
   <ui-view id="ng_subview"></ui-view>
</div>

如您所见,我有一个包含自定义指令和 ui-view 标记的主条目,当在自定义侧边栏中选择链接或使用浏览器的 url 搜索栏时注入内容。

我的 app.config 文件包含路由配置。

$stateProvider
 .state("parent", {
    url: "", //omitted
    template: "main.html",
    controller: "mainController",
    controllerAs: "mainCtrl",
    abstract: true,
    resolved: {} // omitted
 })
 .state("parent.customer_questions", {
    url: "", //omitted
    template: "customer_questions.html",
    controller: "customerQuestionController",
    controllerAs: "customerQestionCtrl",
 })

主控制器:

function(factory, $stateParams){
  let self = this;
  // Http request
  self.form = factory.getData($stateParams.id).then(function(results){
    return results;
  });  
}

问题控制器:

function(customerQuestionsFactory, $stateParams, $filter, data, $scope){
    let self = this;

    self.customerQuestions = {
        questions: [],
        sendReply: function(index, conversation_id){
          /*
          When I call the method it updates the content of the
          customer question view, and it is working correctly.
          However, the custom sidebar directive keeps track of
          the total of new questions, but the sidebar doesn't update.
          I have tried:
          $parent in this case is the mainCtrl
          $scope.$parent.new_question_total = 100 // this does not work.
          */
        }
    }
 }

侧边栏指令:

function($location){
   return {

       restrict: 'E',
       templateUrl: 'custom_sidebar.html',
       replace: true,
       scope: {
         formObject: "="
       },
       link: function($scope, element, attrs) {
          //code omitted
       }
    }
 }

侧边栏 HTML:

<!-- 
    This is where I'm setting the value.
-->
<a ng-click="" ui-sref="" ng-bind="formObject.new_question_total"></a>  

我需要更新侧边栏视图“formObject.new_question_total”中的值。

这是场景: 当我导航到客户问题视图时,有一个更新客户问题的表单,它会更新内容区域。在此页面上,它会跟踪有多少新问题。显然,更新时,新符号会消失。 在侧边栏上,有一个链接可以导航到包含新问题总数的客户问题页面。如果我更新客户视图,这个数字应该会减少。 我知道第一次加载页面时,指令中的链接函数只被调用一次。

  1. 有没有办法从客户问题中调用链接方法 控制器?
  2. 我有哪些选择可以实现这一目标?
  3. 有没有更好的方法来代替调用链接方法?

我不确定您是否需要更多信息,但如果您需要更多信息,我很乐意添加更多信息。 我曾尝试使用 $apply、$watch、$broadcast,在网上查看过,但没有运气。

提前致谢!

【问题讨论】:

    标签: angularjs angular-directive angular-controller


    【解决方案1】:

    使用 $rootScope.$boradcast

    function(customerQuestionsController, $stateParams, $filter, data, $scope, $rootScope){
        let self = this;
    
        self.customerQuestions = {
            questions: [],
            sendReply: function(index, conversation_id){
               $rootScope.$broadcast('questionUpdate',index)
            }
        }
     }
    

    在指令中

     function($location, $rootScope){
       return {
    
           restrict: 'E',
           templateUrl: 'custom_sidebar.html',
           replace: true,
           scope: {
             formObject: "="
           },
           link: function($scope, element, attrs) {
              $rootScope.$on("questionUpdate", function(event, data){
                  //do something
              });
           }
        }
     }
    

    【讨论】:

    • 是的...我安慰了这个值,它正在工作。愚蠢的问题,我如何访问链接方法中的formObject?另外,为什么是 rootScope 而不仅仅是 $scope?
    • questionController 的作用域不是指令作用域的父作用域,只能使用 $rootScope 传递数据
    • 我知道,但是 formObject 来自作为父级的 mainController。我只是好奇我是否可以在链接方法或编译方法中访问 formObject。
    • 是的,您可以在链接功能中以scope.formObject访问它
    • 我确实试过了,值为空。不用担心...感谢您的帮助。你把我带到了我想要的地方......
    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 2014-05-01
    相关资源
    最近更新 更多