指令范围不在$ http.post之后更新

时间:2015-11-21 10:30:15

标签: javascript html angularjs angularjs-directive

我想更新指令范围

我的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 in指令没有更新请给出解决方案

2 个答案:

答案 0 :(得分:2)

您需要$watch超过treeinput而不是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的回调中。

答案 1 :(得分:0)

您正在使用的范围变量是要监视的树是错误的。

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

相关问题