I am trying to initialize $scope.selectedModel to model1with ng-init. However, the following HTML fails to accomplish this:
<div class="productImage">
<div class="imageGallery" ng-init="selectedModel='model1'">
<div ng-repeat="mod in pTab" ng-if="modelIsActive(mod)">
<div ng-repeat="img in mod.galleryImages">
<img class="overviewProductImage" ng-src="{{img.image}}" ng-if="productImageIsActive(img, $index)"/>
</div>
</div>
</div>
</div>
And here's the modelIsActive method that uses selectedModel:
$scope.modelIsActive = function (tab) {
return tab.model== $scope.selectedModel;
}
Eventually I will want to use ng-init="selectedModel= mod.model" but when that didn't work I substituted the simple string 'model1' and found it still wasn't initializing selectedModelto that string.
How can I use ng-init to set $scope.selectedModel? Or should I be using something else? Do I need to use $watch or something similar?
If you can, it is better to initialize your selectedModel in your controller rather than in the HTML using ng-init: this directive can have exepected behaviors.
If you really need to though, you either need to at least define $scope.selectedModel in your controller and then set a value in the ng-init, or use an object instead of directly a value, such as
<div ng-init="model.selectedModel = 'model11'">
(but you'll still need to initialize $scope.model in your controller)
please put your ng-if into the inner of repeat:
<div ng-repeat="mod in pTab">
<div ng-repeat="img in mod.galleryImages" ng-if="modelIsActive(mod)">
Related
I have the following ng-repeat that gives an element a className based on whether it is even or odd...
<div ng-click="displayHTML(content)" ng-class-odd="'title'" ng-class-even="'html'" ng-repeat="content in name.name">
{{content}}
</div>
On ng-click I am calling displayHTML() and passing a parameter content so that only that particular div that is clicked calls the function.
However on ng-click I'm attempting to see whether the clicked element is ng-class-odd or ng-class-even and I want the function to only be called if the element is ng-class-odd.
But I do not know an easy way to do this. Is there an easy "angular" way of doing it. If not, what should I put here...
$scope.displayHTML = function(obj){
////
}
ngRepeat exposes several local variables to the scope, specifically $even and $odd. Just use those:
<div ng-click="displayHTML(content, $even)" ng-class-odd="'title'" ng-class-even="'html'" ng-repeat="content in name.name">
{{content}}
</div>
js:
$scope.displayHTML = function(obj, $even) {
if($even) {/* even code*/}
else {/* odd code */}
}
I am trying to bind the expression used in a ng-show attribute to the result of a jQuery selector. The reason for this is that I need to hide/show an element based on the presence of another element (ng-view)
The markup I have currently is
<div ng-view id="partialView"></div>
<div ng-show="$('#partialView').length === 0">
<h1>MAIN CONTENT</h1>
</div>
and when this is rendered I get:
<!-- ngView: -->
<div ng-show="$('#partialView').length === 0" class="ng-hide">...</div>
when the ng-view is not provided, or
<div ng-view id="partialView">...</div>
<div ng-show="$('#partialView').length === 0" class="ng-hide">...</div>
the expression in the ng-show is not being evaluated (or maybe simply not re-evaluated).
My question is
Is this the correct way to bind the ng-show to the presence of another element in the DOM, and if it is the correct way to do it, what is wrong with my syntax that is stopping it from working?
ng-show work with scope that you defined in controller.
you should define scope with jQuery or scope.
like
$scope.partialView=0 //or any thing that you want.
<div ng-show="partialView === 0" class="ng-hide">...</div>
Your view does not know what $() is, in your controller assign the jquery $ to $scope element e.g. $scope['$'] = $
Thanks to the help of #basant-rules and #martin-glennon I moved the logic into the controller, so now my controller has a function defined on it's scope:
var LandingPageController = function ($scope) {
$scope.noPartialView = function () {
return $('#partialView').length ===0;
};
};
and the view looks like:
<div ng-view id="partialView"></div>
<div ng-show="noPartialView()">
<h1>Main Content</h1>
</div>
I'm using angular grid -
let's say i have a scope object as follows:
$scope.test = 3
If I want to dynamically set an html id, I would do something like this:
<div id="{{test}}"></div>
Checking the DOM, I see the following:
<div id="3"></div>
For my angular grid, I want to do something like this:
<div ag-grid="{{test}}"></div>
Checking the DOM I literally get:
<div ag-grid="{{test}}"></div>
Is there a way around this?
You can use ngAttr and do this like the following:
ng-attr-ag_grid="{{test}}"
Please check if this helps
Use
<div ag-grid="test"></div>
Check following link and check first example html file forag-grid tag with binding variable
Note: Based on example test should be object not string or other type.
Is {{ }} necessary?
You can try:
ag-grid="test"
I want to catch the first iteration in ng-repeat directive:
<div ng-repeat="product in products">
<div is-open="{if "[[$index]]" == 0}true{else}false{/if}">
...
</div>
</div>
But it doesn't work. Setting 0 as string also doesn't work. Comparing in angular also doesn't work.
How can I do that?
Try:
<div is-open="{{$index == 0}}"></div>
You should use special property $first of ng-repeat with ternary operator. Like see below snippet
<div ng-repeat="product in products">
<div is-open="($first) ? true : false">
...
</div>
</div>
I suppose smarty code will not work as you're expecting. Smarty is a server side programming language and angular runs on client side. In your query, smarty tags are unnecessary being called in angular directive. Above trick will work for you.
There are also some couple of special properties available too. You can check them out here
I have a main file which includes a file inside a subfolder using ng-include, like this,
<p ng-include=" 'activity/act01/' "></p>
where the ng-include value will change dynamically
then, how can i get current value of ng-include.
ng-include binds to an expression. This is why your example requires single quotes. It wants an expression returning a string. This can be function returning a string, a variable, or a literal string. Because of this, it is easy to bind ng-include to a scope variable.
Here is an example of how to do it:
function TestCtrl($scope) {
$scope.actPage = 'activity/act01/';
$scope.goToActTwo = function(){
$scope.actPage = 'activity/act02';
};
}
And the HTML:
<div ng-app ng-controller="TestCtrl">
<div ng-include="actPage"></div>
<button ng-click="goToActTwo()">Change Act</button>
</div>