Scope in directive is not updated after $ http.post

I want to update the scope

my filterconditions.js file

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;                  
 }
    }]);

my html page

<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: [] }];

in this situation, scope.tree is updated, but see this

 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: [] }];
      });

in the above case the scope.tree in the directive is not updated, please give a solution

+4
source share
2 answers

You need to have $watchover treeinput, not the treefollowing:

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
})

and all code that depends on treeinputshould be inside the callback for yours $watch.

+2
source

The scope variable used is a tree to observe that it is wrong.

, AngularJS. . , $scope. $Broadcast .

0

Source: https://habr.com/ru/post/1616849/


All Articles