2

I am aware that use of ng-if dictates that ng-if destroys the scope on the element unlike the use of ng-show or ng-hide.. I however need to use ng-if (ng-show / hide is not an option) because I actually need the element to not render on the page when ng-if is falsely..

I use ng-if as part of a directive template..

My directive

app.directive("myDirective", function ($parse) {
return {
    restrict: "E",
    scope:{},
    controller: function ($scope, $element, $attrs) {
        // controller code
    },
    templateUrl: "template.html",
    compile: function(elm, attrs){
        var expFn = $parse(attrs.atr1 + '.' + attrs.atr2);
            return function(scope,elm,attrs){

                scope.$parent.$watch(expFn, function(val){
                    scope.exp = val;
                })

                scope.$watch('exp', function(val){
                    expFn.assign(scope.$parent, val)
                })
            }
    }
}
})

My template

<div ng-click="view = !view">{{exp}}</div>

<div ng-if="view">

    <input type="text" ng-model="exp"><br/>

    <div class="btn btn-default" ng-click="Submit()">Submit</div>

</div>

Is there a way to "bypass" the ng-if behavior,,, to KEEP the scope,, or recreate it?

3 Answers 3

5

I created the attach-if directive for this purpose.

The code is available at https://github.com/Sparks-Creative-Limited/angular-attach-if and, if you use bower, it's registered as a downloadable component with the following details:

angular-attach-if 0.1.0

There is also a demo page to show how it works.

Sign up to request clarification or add additional context in comments.

5 Comments

I will take a look and get back to you thanks! I was about to start writing a modified version of ng-if as well.
Your directive does work in the general context of scope and ng-if. I find it quite useful, thank you. It however does not quite fit with the issue I have.. when I use it, I still get problems with finding element ng-model for some reason. I accepted your answer because it is regardless of my issue, a useful one :)
I referenced you in this question stackoverflow.com/questions/21667778/… ,, I hope you don't mind.
Hi. No, I don't mind. If you would like to raise an issue on the github project with an example, I may be able to improve the attach-if directive for you.
I found a reason why my method wasn't working. I will still go back to yours and take a closer look. I suppose we can find an issue :)
0

If you are using ng-if it won't create the element at all period. So the directive does not exist in that instance. I know you mentioned ng-hide/show is not an option but I think its the only option. What is your reason for not wanting it to render and keep its scope? which by the way defeats the purpose of the directive. If you are doing some logic in the directive that doesn't require the view, it should belong somewhere else.

3 Comments

I am fully aware of what ng-if does and the fact it creates a child scope.. Why I need to use it does not fit on 3 pages of text.. but,, it's a security relate
I am aware of what ng-if does and the fact it creates a child scope.. Why I need to use it does not fit on 3 long pages of text.. but,, it's security related.. The directive clearly has a visible and conditionally visible component (it is by no means invisible).. I am asking a specific question because I need a specific solution.. WHEN rendered I need access to parent scope... (I think I just answered my own question.. I need to reference a $parent model) .. Either way.. An actual answer or a good reference would be helpful.
Sorry for doubled up answer portion,, I accidentally hit the add comment button
0

If the scope has single/few variable, ng-init="obj=$parent.obj" on the same tag where ng-if is, would help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.