【问题标题】:Scope in directive not updating after $http.post$http.post 后指令中的范围未更新
【发布时间】:2015-11-21 18:41:55
【问题描述】:

我想更新指令范围

我的 filterconditions.js 文件

SCAApp
.directive('filtercondition', ['ngDialog',
    function (ngDialog) {
        /**          
        * @name directive.searchdrop
        * @description searchdrop element
        * @param {object} field, required  and options
        * @param {object} value, defaul value of the field, could be null             
        * @param {string} inputClass, optional, optional css class            
        * @returns {object} directive
        */
        return {
            restrict: 'A',
            replace: true,
            scope: {
                conditionsinput: "=",
                tables: "=",
                operatorsinput: "=",
                treeinput: "=treeinput"
            },
            templateUrl: '../../Scripts/app/shared/directives/filterconditions/partials/filterconditions.html',
            link: function (scope) {
                scope.$watch('tree', function () {
                    scope.treeinput = scope.tree;
                });
                scope.generateUid = function () {
                    var d = new Date();
                    var uid = d.getTime();
                    return uid;
                };

                scope.conditions = scope.conditionsinput;

                //scope.tables = scope.tablesinput;

                scope.operators = scope.operatorsinput;

                scope.tree = scope.treeinput;                  
 }
    }]);

我的html页面

<div filtercondition conditionsinput="conditions" tables="tables" operatorsinput="operators" treeinput="tree"></div>

$scope.tree = [{ nodes: [], fields: [], id: $scope.generateUid(), condition: $scope.conditions[0], visibility: true, selectColumns: [] }];

在这种情况下,scope.tree 会更新,但请参阅

 dataFactory.get("/BusinessView/GetBusinessViewById?applicationId=" + $scope.currentAppId + "&viewId=" + $scope.$stateParams.viewId + "&objectId=" + $scope.$stateParams.formId)
      .then(function (result) {
          $scope.tree =[{ nodes: [], fields: [], id: $scope.generateUid(), condition: $scope.conditions[0], visibility: true, selectColumns: [] }];
      });

在上述情况下,指令中的 scope.tree 没有更新,请给出解决方案

【问题讨论】:

  • 为什么你的指令中有treeinput: "=treeinput" 而不是treeinput: "="?我认为你也不需要$watch("tree")

标签: javascript html angularjs angularjs-directive


【解决方案1】:

您需要在treeinput 上使用$watch 而不是tree,如下所示:

scope.$watch('treeinput', function(new_val, old_val) {
    //new_val has the new value of $scope.tree
    //rest of your link function code that depends on treeinput
})

所有依赖于treeinput 的代码都应该在你的$watch 的回调中。

【讨论】:

  • 很高兴为您提供帮助。如果您认为答案正确,请接受答案:)
【解决方案2】:

您使用的范围变量是 tree to watch,这是错误的。

但是,如果您仍然面临这个问题 我希望这纯粹是 AngularJS 错误。我在我的一个项目中也遇到过这个问题。无论如何,您可以在这种情况下使用 $scope.$broadcast。

【讨论】:

    猜你喜欢
    • 2013-06-04
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多